The product collection is becoming more and more important in Magento 2. This is because product collection is now often required in many different filters as well as requirements.
For those who don’t know, getting product collection in Magento 2 is simply means showing the items in your Magento 2 store when you run the command.
Now, you can get product collection in Magento 2 using many different conditions such as:
- Load product collection
- Get product collection
- Filter product collection
- Sort of product collection
In this tutorial, we are going to share the steps to get product collection in Magento 2 with all of the above conditions. Let’s start with the first condition.
Load Collection in Magento 2:
<?php namespace MD\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; } }
NOTE: Please change the code as per your requirement to getProductCollection() function.
Load Product Collection with Specific Attribute
$collection = $this->productCollectionFactory->create(); $collection->addAttributeToSelect(['name','sku']); $collection->setPageSize(3); foreach ($collection as $product) { print_r($product->getData()); }
Load Product Collection with All Attribute
$collection = $this->productCollectionFactory->create(); $collection->addAttributeToSelect('*'); $collection->setPageSize(3); foreach ($collection as $product) { print_r($product->getData()); }
Get Product Collection by Multiple Categories in Magento 2
$categories = [1,2,3]; //category ids array $collection = $this->productCollectionFactory->create(); $collection->addAttributeToSelect('*'); $collection->addCategoriesFilter(['in' => $categories]); return $collection;
Get Product Collection by Specific Category in Magento 2
$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;
Get Product Collection by Product Type in Magento 2
$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;
NOTE: You can also add below text as value of type_id to apply different product type filters:
- Simple - Filter of a simple product
- Configurable - Filter of configurable product
- Grouped - Filter of a grouped product
- Virtual - Filter of virtual product
- Bundle - Filter of bundle product
Get Product Collection by Store ID in Magento 2
$storeid = 1; $collection = $this->productCollectionFactory->create(); $collection->addAttributeToSelect('*'); $collection->addStoreFilter($storeid); return $collection;
Get Product Collection by Website IDs in Magento 2
$website_ids = [1,2]; $collection = $this->productCollectionFactory->create(); $collection->addAttributeToSelect('*'); $collection->addStoreFilter($storeid); $collection->addWebsiteFilter($websiteIds); return $collection;
How to Get Product Collection Filter in Magento 2?
There are multiple filters you can use for product collection.
Let’s look at each of them one by one below.
1 - Is Equal to
$collection = $this->productCollectionFactory->create(); $collection->addAttributeToFilter('status', ['eq' => 1]);
2 - Is Not Equal to
$collection = $this->productCollectionFactory->create(); $collection->addAttributeToFilter('status', ['neq' => 1]);
3 - Greater than
$collection = $this->productCollectionFactory->create(); $collection->addAttributeToFilter('price', ['gt' => 100]);
4 - Greater than Equal to
$collection = $this->productCollectionFactory->create(); $collection->addAttributeToFilter('price', ['gteq' => 100]);
5 - Less than
$collection = $this->productCollectionFactory->create(); $collection->addAttributeToFilter('price', ['lt' => 100]);
6 - Less than Equal to
$collection = $this->productCollectionFactory->create(); $collection->addAttributeToFilter('price', ['lteq' => 100]);
7 - Like
$collection = $this->productCollectionFactory->create(); $collection->addAttributeToFilter('sku', ['like' => '%Bag%']);
8 - Not Like
$collection = $this->productCollectionFactory->create(); $collection->addAttributeToFilter('sku', ['nlike' => '%Bag%']);
9 - In Array
$collection = $this->productCollectionFactory->create(); $collection->addAttributeToFilter('entity_id', ['in' => [1,2]]);
10 - Not in Array
$collection = $this->productCollectionFactory->create(); $collection->addAttributeToFilter('entity_id', ['nin' => [1,2]]);
11 - NULL
$collection = $this->productCollectionFactory->create(); $collection->addAttributeToFilter('description', ['null' => true]);
12 - Not NULL
$collection = $this->productCollectionFactory->create(); $collection->addAttributeToFilter('description', ['notnull' => true]);
How to Sort Product Collection in Magento 2?
Just like filters, there are multiple ways to sort your product collection. Let’s look at them below.
1 - Order by ASC
$collection = $this->productCollectionFactory->create(); $collection->getSelect->order('sku', 'ASC');
Or
$collection = $this->productCollectionFactory->create(); $collection->setOrder('sku', 'ASC');
2 - Order by DESC
$collection = $this->productCollectionFactory->create(); $collection->getSelect->order('sku', 'DESC');
Or
$collection = $this->productCollectionFactory->create(); $collection->setOrder('sku', 'DESC');
3 - Set Limit Product Collection
$collection = $this->productCollectionFactory->create(); $collection->setPageSize(50)->load();
4 - Set Limit Product Collection with Current Page
$collection = $this->productCollectionFactory->create(); $collection->setPageSize(50)->setCurPage(2)->load();
5 - Count Product Collection
$collection = $this->productCollectionFactory->create(); echo $collection->count();
6 - Group by Product Collection
$collection = $this->productCollectionFactory->create(); $collection->getSelect()->group('entity_id');
7 - Print Collection Query
$collection = $this->productCollectionFactory->create(); echo $collection->getSelect()->__toString();
Final Words…
And there you have it! These are all the ways you can get a product collection in Magento 2.
We hope that you found this tutorial helpful. And as always, if you get stuck or simply need our professional assistance with Magento 2 Development, you can reach out to us for the same.
Also read:
- How to Get Product Collection by Product ID in Magento 2
- How to Get Recently Viewed Product Collection of a Customer in Magento 2
- How to Get Product Collection by Product SKU in Magento 2
- How to Get Related Products Collection in Magento 2
- How to Get Orders Collection between a Date Range in Magento 2
- How to Get Order Item Collection by Item ID in Magento 2