How to Get Product Collection in Magento 2?

In this blog, I am going to show you how to get the list of all products in Magento 2 programmatically.

In the below example, I will be using both Dependency Injection (DI) and Object Manager to get the product collection in Magento 2.

Using Dependency Injection (DI)

Here is the example code to get the list of all products in Magento 2 using dependency injection.

In this, we might need to inject the object of \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory class in the constructor of our module’s block class and access it from the view ( .phtml ) file.

Sample File Path: app/code/YourCompanyName/YourModuleName/Block/YourCustomBlock.php

<?php
namespace YourCompanyName\YourModuleName\Block;
class YourCustomBlock extends \Magento\Framework\View\Element\Template
{ 
    protected $_productCollectionFactory;
 
    protected $_productVisibility;
 
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context, 
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Catalog\Model\Product\Visibility $productVisibility,
        array $data = []
    ) {
        $this->_productCollectionFactory = $productCollectionFactory; 
        $this->_productVisibility = $productVisibility; 
        parent::__construct($context, $data);
    }
 
    public function getProductCollection() {
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
 
        // filter current website products
        $collection->addWebsiteFilter();

        // filter current store products
        $collection->addStoreFilter();
 
        // set visibility filter
        $collection->setVisibility($this->productVisibility->getVisibleInSiteIds());
 
        // fetching only 5 products
        $collection->setPageSize(5); 
 
        return $collection;
    }
}

Now, we can use the functions in our view (.phtml) file as follows.

$productCollection = $block->getProductCollection(); 
foreach ($productCollection as $product) { 
    echo $product->getId() . '<br />';
    echo $product->getName() . '<br />';
}

Using Object Manager

Here is the code to get the list of all products in Magento 2 using Object Manager.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
 
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToSelect('*');

// filter current website products
$collection->addWebsiteFilter();

// filter current store products
$collection->addStoreFilter();

// set visibility filter
$collection->setVisibility($objectManager->get('\Magento\Catalog\Model\Product\Visibility')->getVisibleInSiteIds());

// fetching only 5 products
$collection->setPageSize(5); 
    foreach ($productCollection as $product) { 
    echo $product->getId() . '<br />';
    echo $product->getName() . '<br />';
}

Read: How to Get Category Collection in Magento 2?

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *