In this blog, we will explain how to get the product collection by using a factory method into a magento 2 block file.
Several types of product collection like collection, filter, sorting, etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
namespace Vendorname\Modulename\Block; class Filename 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; } } |
Filter Product Collection:
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 |
$collection = $this->productFactory->create(); $collection->addAttributeToFilter(‘price’, array(‘lteq’ => 50)); |
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)); |
Like
1 2 |
$collection = $this->productFactory->create(); $collection->addAttributeToFilter(‘sku’, array(‘like’ => ‘%men%’)); |
Not Like
1 2 |
$collection = $this->productFactory->create(); $collection->addAttributeToFilter(‘sku’, array(‘nlike’ => ‘%men%’)); |
In Array
1 2 |
$collection = $this->productFactory->create(); $collection->addAttributeToFilter(‘entity_id’, array(‘in’ => array(10,25,46))); |
Not In Array
1 2 |
$collection = $this->productFactory->create(); $collection->addAttributeToFilter(‘entity_id’, array(‘nin’ => array(10,25,46))); |
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
1 2 |
$collection = $this->productFactory->create(); echo $collection->count(); |
Collection Query Print
1 2 |
$collection = $this->productFactory->create(); echo $collection->getSelect()->__toString(); |
That’s it.
I hope you understand our blog and if you still have any problems you can let us know in the comments section.
Thank you!