Magento 2 Get Product Collection with Filters
In this article I'll let you know about different types of filters which we can use it when we get product collection. As of now we'll get filtered product collection in a block file and after that I'll explain show you all the different types of filters with it's syntext.
Let's get started!!
Check the below code for get product collection with filter in block file.
saveCopyzoom_out_mapnamespace Devhooks\Module\Block;
class Module extends \Magento\Framework\View\Element\Template
{
protected $productFactory;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productFactory,
array $data = []
)
{
$this->productFactory = $productFactory;
parent::__construct($context, $data);
}
public function getProductCollection()
{
$collection = $this->productFactory->create();
$collection->addAttributeToFilter('status', \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
$collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
return $collection;
}
}
In above code I have filter Status and Visibility in a normal way but you can filter the product collection in a following ways.
- Greater than
- Greater than or Equal To
- Less than
- Less than or Equal To
- Is equal to
- Is not equal to
- Like
- Not Like
- In Array
- Not In Array
- NULL
- Not NULL
Greater than
saveCopyzoom_out_map$collection = $this->productFactory->create(); $collection->addAttributeToFilter('price', array('gt' => 25));
Greater than or Equal To
saveCopyzoom_out_map$collection = $this->productFactoryn->create(); $collection->addAttributeToFilter('price', array('gteq' => 25));
Less than
saveCopyzoom_out_map$collection = $this->productFactory->create(); $collection->addAttributeToFilter('price', array('lt' => 25));
Less than or Equal To
saveCopyzoom_out_map$collection = $this->productFactory->create(); $collection->addAttributeToFilter('price', array('lteq' => 25));
Is equal to
saveCopyzoom_out_map$collection = $this->productFactory->create(); $collection->addAttributeToFilter('status', array('eq' => 1));
Is not equal to
saveCopyzoom_out_map$collection = $this->productFactory->create(); $collection->addAttributeToFilter('status', array('neq' => 1));
Like
saveCopyzoom_out_map$collection = $this->productFactory->create(); $collection->addAttributeToFilter('sku', array('like' => '%tshirt%'));
Not Like
saveCopyzoom_out_map$collection = $this->productFactory->create(); $collection->addAttributeToFilter('sku', array('nlike' => '%tshirt%'));
In Array
saveCopyzoom_out_map$collection = $this->productFactory->create(); $collection->addAttributeToFilter('entity_id', array('in' => array(15,23,43)));
Not In Array
saveCopyzoom_out_map$collection = $this->productFactory->create(); $collection->addAttributeToFilter('entity_id', array('nin' => array(15,23,43)));
NULL
saveCopyzoom_out_map$collection = $this->productFactory->create(); $collection->addAttributeToFilter('description', array('null' => true));
Not NULL
saveCopyzoom_out_map$collection = $this->productFactory->create(); $collection->addAttributeToFilter('description', array('notnull' => true));
In this article I have tried to list-out all types of filters with its systext. So, now I hope you can filter your collection as per your need.
That’s it.
I hope this post helped you to find what you were looking for. Bookmark it for your future reference. Do comment below if you have any other questions on that.
P.S. Do share this note with your team.
AI-Powered Recommended Articles
Magento 2 Get Product Collection By Cetegory ID
Learn how to retrieve a product collection by category ID in Magento 2 for customized product listings and filters.
Get Product Collection with a Specific Attributes in Magento 2
Learn how to retrieve product collections with specific attributes in Magento 2 using custom queries.
How to Get Product Collection in Magento 2
Learn how to get product collections in Magento 2 and filter them based on various parameters.
How to Get Salable Quantity in Magento 2
Learn how to retrieve the salable quantity of a product in Magento 2 to ensure accurate inventory tracking and display on your site.
How to Get Current Product in Magento 2
Learn how to retrieve the current product in Magento 2 to dynamically display product-specific information on your website.
How to Get Current Category in Magento 2
Learn how to retrieve the current category in Magento 2 using code to display relevant category data in your store.