How to get product collection in Magento 2

Get product collection in Magento 2 means showing the items in your store when you run the command. With the code snippet in this topic, you can request the specific number of the product as you need.

Table Content

Get Collection in Magento 2 means showing the items in your store when you run the command. With the code snippet in this topic, you can request the specific number of the product as you need. Let’s start calling the product in your Magento 2 store now!

Overview of getting product collection in Magento 2

  • Step 1: Declare in Mageplaza_HelloWorld Block
  • Step 2: Display product collection in phtml file

Step 1: Declare in Mageplaza_HelloWorld Block

You will use a block class of the module Mageplaza_HelloWorld, then possibly inject the object of \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory in the constructor of the module’s block class.

app/code/Mageplaza/HelloWorld/Block/HelloWorld.php

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

You can request the number of the product collection, that is a limited or unlimited number.

Step 2: Display product collection in phtml file

Print out the product collection in phtml file with the below code:

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

If that you have any queries about the article or any questions in general, use the comment section below!

Related Post

Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
how-to
get
product
collection

Sam Thomas
Sam Thomas

CEO and Founder of Mageplaza. Pursueing a simple and healthy lifestyle. A friend, a husband and a dad of two children, a trainer and a wanna-be influencer. He is a big fan of sports and travel, also.

People also searched for

  • magento 2 how to get product collection
  • magento 2 get product collection
  • get product collection magento 2
  • get product collection in magento 2
  • magento 2 product collection
  • magento2 get product collection
  • magento 2 get all products
  • magento 2 collection
  • how to get product collection in magento 2
  • magento 2 get collection
  • get all product collection in magento 2
  • magento 2 collection factory
  • magento 2 get all product collection
  • magento 2 product collection factory
  • magento 2 get all products programmatically
  • magento 2 get product collection using object manager
  • magento 2 get product collection by sku
  • magento 2 get products collection
  • how to get product collection in magento 2 programmatically
  • magento 2 custom product list
  • magento 2 get product collection by product id
  • magento 2 get product collection filter by attribute value
  • magento 2 get all products collection
  • magento 2 get featured products collection
  • magento 2 load product collection
  • magento 2 get product collection by ids
  • magento 2 product collection filter by product id
  • magento 2 get product collection filter by attribute
  • magento 2 collection limit