Different ways to get Product Collection in Magento2

Posted on 30 March, 2019

In day to day development of Magento, every Developer requires products collection, somewhere in every project, a developer requires a product collection in different filters and requirements. Here I have noted down mostly all type of filters to get product collection with conditions. In this post, we will see all type of product collection like a collection, filter, sorting, etc.

To get  product collection using a factory method:

1
2
3
4
5
6
7
8
9
10
11
12
13
protected $productFactory;
protected $categoryFactory;
public function __construct(
     ....
     \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productFactory,
  \Magento\Catalog\Model\CategoryFactory $categoryFactory
     ....
){
    ....
    $this->collectionFactory = $productFactory;
  $this->categoryFactory = $categoryFactory;
    ....
}

get all products using page size:-

1
2
3
4
5
$collection = $this->productFactory->create();
$collection->setPageSize(3);
foreach ($collection as $product) {
    print_r($product->getData());
}

Load product collection with all attribute:-

1
2
3
4
5
$collection = $this->productFactory->create();
$collection->addAttributeToSelect('*');
foreach ($collection as $product) {
    print_r($product->getData());
}

Load product collection with a specific attribute:-

1
2
3
4
5
6
7
$collection = $this->productFactory->create();
$collection->addAttributeToSelect('name');
$collection->addAttributeToSelect('price');
foreach ($collection as $product) {
    print_r($product->getData());
}

Load product collection by category:-

1
2
3
4
5
6
7
8
9
10
$categoryId = '9';
$category = $this->categoryFactory->create()->load($categoryId);
$collection = $this->productFactory->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);
foreach ($collection as $product) {
    print_r($product->getData());
}

Filter Product Collection:

Is equal to:-

1
2
$collection = $this->productFactory->create();
$collection->addAttributeToFilter('status', array('eq' => 1));

Is not equal to:-

1
2
$collection = $this->productFactory->create();
$collection->addAttributeToFilter('status', array('neq' => 1));

Greater than:-

1
2
$collection = $this->productFactory->create();
$collection->addAttributeToFilter('price', array('gt' => 50));

Greater than or Equal To:-

1
2
$collection = $this->productFactoryn->create();
$collection->addAttributeToFilter('price', array('gteq' => 50));

Less than:-

1
2
$collection = $this->productFactory->create();
$collection->addAttributeToFilter('price', array('lt' => 50));

Less than or Equal To:-

1
2
3
   $collection = $this->productFactory->create();
$collection->addAttributeToFilter('price', array('lteq' => 50));

 Like:-

1
2
$collection = $this->productFactory->create();
$collection->addAttributeToFilter('sku', array('like' => '%ABC%'));

 Not Like:-

1
2
$collection = $this->productFactory->create();
$collection->addAttributeToFilter('sku', array('nlike' => '%ABC%'));

 In Array:-

1
2
$collection = $this->productFactory->create();
$collection->addAttributeToFilter('entity_id', array('in' => array(1,2,3)));

 Not In Array:-

1
2
$collection = $this->productFactory->create();
$collection->addAttributeToFilter('entity_id', array('nin' => array(1,2,3)));

 NULL:-

1
2
$collection = $this->productFactory->create();
$collection->addAttributeToFilter('description'array('null' => true));

 Not NULL:-

1
2
$collection = $this->productFactory->create();
$collection->addAttributeToFilter('description'array('notnull' => true));

Sort Product Collection:-

 Order by Ascending:-

1
2
$collection = $this->productFactory->create();
$collection->setOrder('price', 'ASC');

 Order by Descending:-

1
2
$collection = $this->productFactory->create();
$collection->setOrder('price', 'DESC');

Limit Product Collection:-

Limit:-

1
2
$collection = $this->productFactory->create();
$collection->setPageSize(50)->load();

Limit with current page:-

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

Count Collection:-

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

Count:-

1
2
$collection = $this->productFactory->create();
echo $collection->count();

Print Collection Query:-

1
2
$collection = $this->productFactory->create();
echo $collection->getSelect()->__toString();

I hope this Magento technical note helped you find what you were looking for. bookmark it for your future reference. Do comment below if you have any queries.


Nimita Gajera , eCommerce Project Manager

Magento Technical Notes

About Emipro

Being an emerging leader in IT market since 2011, Emipro Technologies Pvt. Ltd. has been providing a wide range of business solutions in Odoo & Magento. We are pleased to have a large pool of contented customers with our meticulous work in the domain of ERP & e-Commerce. Our customers are companies of all sizes ranging from startups to large enterprises who realize that they need a professional internet solution to generate revenue streams, establish proper communication channels, to achieve desired goals and streamline business operations. [....] Read More

Our writings seems informative ?

Subscribe for our Magento Technical Notes and get more amazing stuff directly to your inbox!

Post Your Review

X

Your Review has been posted

1 Comment(s)

Peter Parker

Posted on 5 July, 2019

This is very useful, thanks alot.