How to Get Product Collection in Magento 2

Magento 2 is the perfect platform for E-commerce today. Because E-commerce store has to deal with products, and Magento 2 offers various features and functionalities to work with products. Today, we'll see how to get product collection in Magento 2. First of all, let me clear the term: why get product collection? Get product collection means showing items/products in-store when you run the command.

Steps To Get How to Get Product Collection in Magento 2

There are many different ways and strategies to get product collection.

  • Load
    • with specific attribute
    • with all attributes
  • Get
    • with product type
    • with multiple categories and specific category
    • also with store id
  • Filter
    • with many different options given below
  • Sort
    • Ascending
    • Descending

Load Product Collection in Magento 2:-

<?php
namespace MP\Hello\Block;
class Hello extends \Magento\Framework\View\Element\Template
{   
    protected $productCollectionFactory;
    protected $categoryFactory;
    public function __construct(
       \Magento\Framework\View\Element\Template\Context $context,
       \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
       \Magento\Catalog\Model\CategoryFactory $categoryFactory,
       array $data = []
    ){
       $this->productCollectionFactory = $productCollectionFactory;
       $this->categoryFactory = $categoryFactory;
       parent::__construct($context, $data);
    }  
    public function getProductCollection()
    {
       $collection = $this->productCollectionFactory->create();
     //$collection->addAttributeToSelect('*'); //For all attributes
     //$collection->addAttributeToSelect(['name','sku']); // For specific attributes
       $collection->setPageSize(5);      
       foreach ($collection as $product) {
           print_r($product->getData());
       }
        return $collection;
    }
}

Get Product Collection in Magento 2:-

With Product type:

$collection = $this->productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addAttributeToFilter('type_id', ['eq' => 'simple']);
$collection->getSelect()->order('created_at', \Magento\Framework\DB\Select::SQL_ASC);
$collection->getSelect()->limit(15);
return $collection;

With multiple categories:

$categories = [1,2,3,4,5]; //category ids array
$collection = $this->productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addCategoriesFilter(['in' => $categories]);
return $collection;

With specific category:

$categoryId = '15';
$category = $this->categoryFactory->create()->load($categoryId);
$collection = $this->productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addCategoryFilter($category);
$collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
$collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
return $collection;

With Store id:

$storeid = 15;
$collection = $this->productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addStoreFilter($storeid);
return $collection;

Filter Product collection:-

Equal to & Not equal to:

$collection = $this->productCollectionFactory->create();
$collection->addAttributeToFilter('status', ['eq' => 1]); //Equal to

$collection = $this->productCollectionFactory->create();
$collection->addAttributeToFilter('status', ['neq' => 1]); //Not equal to

Greater than, Greater and equal & Less than, Lesser and equal:

$collection = $this->productCollectionFactory->create();
$collection->addAttributeToFilter('price', ['gt' => 100]); //Greater than

$collection = $this->productCollectionFactory->create();
$collection->addAttributeToFilter('price', ['gteq' => 100]); //Greater and Equal

$collection = $this->productCollectionFactory->create();
$collection->addAttributeToFilter('price', ['lt' => 100]); //Less than

$collection = $this->productCollectionFactory->create();
$collection->addAttributeToFilter('price', ['lteq' => 100]); //Lesser and equal

Like, not like:

$collection = $this->productCollectionFactory->create();
$collection->addAttributeToFilter('sku', ['like' => '%Bag%']); //Like

$collection = $this->productCollectionFactory->create();
$collection->addAttributeToFilter('sku', ['nlike' => '%Bag%']); //Not like

In Array, not in array:

$collection = $this->productCollectionFactory->create();
$collection->addAttributeToFilter('entity_id', ['in' => [1,2]]); //In array

$collection = $this->productCollectionFactory->create();
$collection->addAttributeToFilter('entity_id', ['nin' => [1,2]]); //Not in array

Null, not Null:

$collection = $this->productCollectionFactory->create();
$collection->addAttributeToFilter('description',  ['null' => true]); //NULL

$collection = $this->productCollectionFactory->create();
$collection->addAttributeToFilter('description',  ['notnull' => true]); //Not NULL

Sort Product Collection:-

Ascending & Descending:

$collection = $this->productCollectionFactory->create();
$collection->getSelect->order('sku', 'ASC'); //Ascending

$collection = $this->productCollectionFactory->create();
$collection->getSelect->order('sku', 'DESC'); //Descending

⇒ Hope this information will be helpful to you. Still, any confusion? Contact us for solutions.

Also, Read