skip to Main Content

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

$objectManager->get('MagentoCatalogModelProduct')
            ->getCollection()
            ->addAttributeToSelect('*');

But only simple products are returned.

Getting the NOT RETURNED products by sku works fine:

$p = $objectManager->create('MagentoCatalogModelProductRepository')->get($sku);

Any idea?
Thanks in advance.


Magento version 2.2.0

2

Answers


  1. 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('MagentoCatalogModelProductFactory')->create()
            ->getCollection()
            ->addAttributeToSelect('*');
    
    Login or Signup to reply.
  2. Try this

    In your block file

    <?php
    namespace MageplazaHelloWorldBlock;
    class HelloWorld extends MagentoFrameworkViewElementTemplate
    {    
        protected $_productCollectionFactory;
    
        public function __construct(
            MagentoBackendBlockTemplateContext $context,        
            MagentoCatalogModelResourceModelProductCollectionFactory $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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search