3

I get product collection using \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory in block( dependency injection), the result doesn't have name attribute,

The similar Q&A(ObjectManager) here Product collection has no name attribute how can I get it magento2? doesn't work.

The detail codes is below, my question is "how to add name attribute in the block collection"?


Detail codes

In block:

<?php
namespace Vendor\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('*');
        $collection->setPageSize(3); // fetching only 3 products
        return $collection->getData();
    }
}

In template (.phtml) file:

<?php
$productCollection = $block->getProductCollection();
echo "<pre>";
print_r($productCollection);
echo "</pre>";

Result:

Array
(
    [0] => Array
        (
            [entity_id] => 1
            [attribute_set_id] => 15
            [type_id] => simple
            [sku] => 24-MB01
            [has_options] => 0
            [required_options] => 0
            [created_at] => 2017-08-14 07:07:01
            [updated_at] => 2017-08-14 07:07:01
        )

    ...

)
0
9

There are attributes that may not work properly with getData() such as the product price because getData() contains only main table data.

Return full collection object(without getData()) in getProductCollection() function

public function getProductCollection()
{
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    $collection->setPageSize(3); // fetching only 3 products
    return $collection;
}

Now you can get product id, name, sku and other information with getter function like getId(), getName(), getSku(), ... in phtml

<?php

$productCollection = $block->getProductCollection();

foreach ($productCollection as $product) {
    echo $product->getId();
    echo $product->getName();
}
4
  • Upvote. But strange, my last question magento.stackexchange.com/questions/191300/… collection without getData() will result Invalid argument supplied for foreach(), why it works now? I am confused.
    – Key Shang
    Aug 31 '17 at 9:22
  • Becuase you use getData() in foreach $product object in previous question and in this question you use getData() on whole collection object Aug 31 '17 at 9:26
  • Hi @PrincePatel, Can we filter the media_gallery direct in product collection because I have more then 20,000 products, so if try to load each product so it will take so much time. You have any idea?
    – Rajan Soni
    Jan 11 '18 at 6:58
  • Yes you can filter the collection by image like: $collection->addAttributeToFilter('small_image',array('notnull'=>'','neq'=>'no_selection')); in product collection Jan 11 '18 at 8:26
2

You need to use Getter functions getName()

From

echo "<pre>";
print_r($productCollection);
echo "</pre>";

you don't get data from above code.print_r($productCollection); Only given data of product model of main table catalog_product_entity

You need to run foreach loop for getting name and inside it you will get your desire result

foreach ($productCollection as $product) {
echo $product->getName()

}

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.