I want to fetch all products in my ecommerce and all its attributes. How can I do that? For example, I want to be able to call a function that gets all products, an then one by one fetch all the attributes of that product.


You can try below code for your requirement.

namespace Test\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();
    return $collection;

Display in phtml using block method

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

  • What if I want to fetch also all attributes that are not in the default attribute set? Will this work too? – dirac Jan 28 '19 at 14:35
  • Yes. It will fetch all attributes – Rizwan Khan Jan 28 '19 at 18:34

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.