简体   繁体   English

Magento2获得产品系列

[英]Magento2 get products collection

I am trying to get store view specific product attribute values for all(actually filtered list but that's irrelevant) products like so: 我正在尝试获取所有商店视图特定的产品属性值(实际过滤的列表,但这是不相关的)产品,如下所示:

<?php

/* @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory */
$collection = $collectionFactory->create()->setStoreId(3)->load();

foreach ($collection as $product) {
    var_dump($product->getSku());
    var_dump($product->getName());
}

SKU is returned as expected(as it's general anyway) but product name is not - instead NULL is returned. SKU按预期返回(因为它是一般的)但产品名称不是 - 而是返回NULL

It works fine in adminhtml product edit controller where builder is used. 它在使用构建器的 adminhtml产品编辑控制器中工作正常。 The only difference is that edit controller works with Model\\Product model; 唯一的区别是编辑控制器适用于Model\\Product模型; collection for some reason works with Model\\Product\\Interceptor . 集合由于某种原因适用于Model\\Product\\Interceptor Have tried to load each product in the loop using Model\\ProductFactory (which is how it is done in edit controller) - doing so $product->getName() returns corresponding value(however I was not able to get other relevant product attributes). 尝试使用Model\\ProductFactory (这是在编辑控制器中完成的方式)在循环中加载每个产品 - 这样做$product->getName()返回相应的值(但是我无法获得其他相关的产品属性) 。 Loading product once again in the loop is wrong off course. 当然在环路中再次装载产品是错误的。

So the question : What is the expected way of retrieving products and getting product attribute(description, weight etc) values for given store view? 所以问题是 :对于给定的商店视图,检索产品和获取产品属性(描述,重量等)值的预期方式是什么?

<?php

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');

$collection = $productCollection->create()
            ->addAttributeToSelect('*')
            ->load();

foreach ($collection as $product){
    echo 'Name  =  '.$product->getName().'<br>';
}  

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM