0

我正在尝试获得所有产品。我正在这样做:

$objectManager->get('Magento\Catalog\Model\Product')
            ->getCollection()
            ->addAttributeToSelect('*');

但只退回简单的产品。

通过 sku 获取 NOT RETURNED 产品可以正常工作:

$p = $objectManager->create('\Magento\Catalog\Model\ProductRepository')->get($sku);

任何想法?提前致谢。

-- Magento 2.2.0 版

4

2 回答 2

0

尝试这个

在你的块文件中

<?php
namespace Mageplaza\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{    
    protected $_productCollectionFactory;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,        
        array $data = []
    )
    {    
        $this->_productCollectionFactory = $productCollectionFactory;    
        parent::__construct($context, $data);
    }

    public function getProductCollection()
    {
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        return $collection;
    }
}
?>

和你的 .phtml 文件

$productCollection = $block->getProductCollection();
foreach ($productCollection as $product) {
    print_r($product->getData());     
    echo "<br>";
}

它提供了分组和可配置的产品,它也为我工作,请试试这个

你也可以试试这个链接: https ://www.mageplaza.com/how-get-product-collection-magento-2.html

于 2018-04-30T11:07:02.037 回答
0

Magento2 有概念调用工厂,它是调用模型对象的背景。所以我认为你应该在调用对象模型之前新建工厂。

$objectManager->get('\Magento\Catalog\Model\ProductFactory')->create()
        ->getCollection()
        ->addAttributeToSelect('*');
于 2018-04-20T02:48:51.720 回答