0
votes

I'm trying to get all the products. I'm doing this way:

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

But only simple products are returned.

Getting the NOT RETURNED products by sku works fine:

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

Any idea? Thanks in advance.

-- Magento version 2.2.0

2

2 Answers

0
votes

Magento2 has concept call Factory, it is background to call model object. So I think you should new Factory before calling object model.

$objectManager->get('\Magento\Catalog\Model\ProductFactory')->create()
        ->getCollection()
        ->addAttributeToSelect('*');
0
votes

Try this

In your block file

<?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;
    }
}
?>

and Your .phtml file

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

it gives the grouped and configurable product also it wikk work for me please try this

you can try also this link: https://www.mageplaza.com/how-get-product-collection-magento-2.html