1

I'm trying to show all of my products in a custom controller when I'm using the collection in a loop, the loop doesn't initiate at all. but when I'm printing $collection->getData() I get an array filled with product entity table values.

Another thing is I cannot add product attributes (such as name) to the collection, 'addAttributeToSelect' doesn't add anything.

public function __construct(
    Magento\Framework\App\Action\Context $context,
    Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
)
{
    parent::__construct($context);
    $this->_productCollectionFactory = $productCollectionFactory;
}

public function execute()
{
    $collection = $this->_productCollectionFactory->create();
    print_r($collection->getData()); // print's out array
    foreach ($collection as $product) { // doesn't loop
        var_dump($product->getData());
    }
}
  • 1
    try printing array_keys($product->getData()) to see if it prints out anything. printing $product->getData() will result probably in a memory issue since it tries to print large objects. – Marius Dec 16 '19 at 12:28
  • I tried to echo 1 at the start of the loop to see if it's even entering the loop, and it didn't. – Shalev Dec 17 '19 at 12:20
0

You can use this.

public function execute()
{
    $collection = $this->productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    //$this->collection->addAttributeToSelect(['entity_id', 'firstname', 'lastname', 'email']);
    //$collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
    //$collection->setVisibility($this->productVisibility->getVisibleInSiteIds());

    $collection->load();

    //$collection->addCategoryIds();
    //$collection->addMediaGalleryData();
    //$collection->addMinimalPrice();
    //$collection->addFinalPrice();

    $products = $collection->getItems();
    foreach($products as $product)
    {
        print_r($product->getData());
    }
}
|improve this answer|||||
  • Hey, thanks tried it, didn't work. I don't think you have to load the collection in order to loop over it. and getItems() returns the same result. – Shalev Dec 17 '19 at 12:23
  • I have using this code for product feeds for google and facebook without any error. You need comment out addFinalPrice line for price data. – cmbilisim Dec 17 '19 at 15:26
0

OK, this is a bit stupid but, The issue was I tried to loop over a product collection when all the products were on disabled (!). It was a little misleading because when I wrote getData it showed all the products, but didn't even enter the loop.

I'd be happy if someone could explain why Magento auto removes disabled products out of the collection, maybe it has something to do with the fact that it was all written in a front controller?

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.