Magento 2

Magento 2 : Get Product Collection

In this technical blog, we will see different ways of get product collection in Magento 2. Nowadays, Somewhere in every project developers need to get product collection with many conditions. In other words, There are lots of ways and filters available to get product collection for details of products. Here, we will see how to get product collection with filters, sorting, etc.

First of all, let’s do how to load product collection. To create custom module, you can click here.

1) Load product collection :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

<?php
/**
 * Created By : Rohan Hapani
 */
namespace RH\HelloWorld\Block;

class HelloWorld 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->setPageSize(3);
        foreach ($collection as $product)
        {
            print_r($product->getData());
        }
        return $collection;
    }
}

[/dm_code_snippet]

Firstly, change below all types of code as per requirement in getProductCollection() function.

2) Load product collection with specific attribute :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

$collection = $this->productCollectionFactory->create();
$collection->addAttributeToSelect(['name','sku']);
$collection->setPageSize(3);
foreach ($collection as $product)
{
    print_r($product->getData());
}

[/dm_code_snippet]

3) Load product collection with all attribute :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

$collection = $this->productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->setPageSize(3);
foreach ($collection as $product)
{
    print_r($product->getData());
}

[/dm_code_snippet]

4) Get product collection by multiple categories :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

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

[/dm_code_snippet]

5) Get product collection by specific category :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

$categoryId = '1';
$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;

[/dm_code_snippet]

6) Get product collection by product type :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

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

[/dm_code_snippet]

You can add below text also as value of type_id for different product type filter :

  • simple = Filter of simple product
  • configurable = Filter of configurable product
  • grouped = Filter of grouped product
  • virtual = Filter of virtual product
  • bundle = Filter of bundle product

7) Get product collection by store id :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

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

[/dm_code_snippet]

8) Get product collection by website ids :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

$website_ids = [1,2];
$collection = $this->productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addStoreFilter($storeid);
$collection->addWebsiteFilter($websiteIds);
return $collection;

[/dm_code_snippet]

9) Filter Product Collection :

Is equal to :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

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

[/dm_code_snippet]

Is not equal to :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

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

[/dm_code_snippet]

Greater than :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

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

[/dm_code_snippet]

Greater than equal to :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

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

[/dm_code_snippet]

Less than :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

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

[/dm_code_snippet]

Less than equal to :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

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

[/dm_code_snippet]

Like :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

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

[/dm_code_snippet]

Not Like :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

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

[/dm_code_snippet]

In Array :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

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

[/dm_code_snippet]

Not in Array :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

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

[/dm_code_snippet]

NULL :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

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

[/dm_code_snippet]

NOT NULL :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

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

[/dm_code_snippet]

Sort Product Collection :

Order by ASC :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

$collection = $this->productCollectionFactory->create();
$collection->setOrder('sku', 'ASC');

[/dm_code_snippet]

OR

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

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

[/dm_code_snippet]

Order by DESC :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

$collection = $this->productCollectionFactory->create();
$collection->setOrder('sku', 'DESC');

[/dm_code_snippet]

OR

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

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

[/dm_code_snippet]

Set Limit Product Collection :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

$collection = $this->productCollectionFactory->create();
$collection->setPageSize(50)->load();

[/dm_code_snippet]

Set Limit Product Collection with Current Page :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

$collection = $this->productCollectionFactory->create();
$collection->setPageSize(50)->setCurPage(2)->load();

[/dm_code_snippet]

Count Product Collection :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

$collection = $this->productCollectionFactory->create();
echo $collection->count();

[/dm_code_snippet]

Group by Product Collection :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

$collection = $this->productCollectionFactory->create();
$collection->getSelect()->group('entity_id');

[/dm_code_snippet]

Print Collection Query :

[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

$collection = $this->productCollectionFactory->create();
echo $collection->getSelect()->__toString();

[/dm_code_snippet]

I hope this technical blog helped you find all ways of get product collection in Magento 2. Save this page for your future reference. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution 🙂

Tagged , ,