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_map
namespace 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.

Review other articles maybe it'll help you too.




Recent Articles
Tags
Newsletter
Chrome Extension
Copyright © 2024 devhooks.in All rights reserved.
Ads OFF toggle_off
wifi_off