In this Artical, we will learn how to get product collection in different ways/situations. These days, developer need to get custom product collection with selected fields, filter collection, sorting collection.
so, we represent how to play with product collection as per your requirement.
Let’s start to load product collection.
How to get Product Collection in class
<?php
namespace Mageclues\Custom\Block;
use Magento\Framework\View\Element\Template\Context;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\View\Element\Template\Context;
class Product extends \Magento\Framework\View\Element\Template
{
protected $_productCollectionFactory;
public function __construct(
Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
array $data = []
) {
$this->productCollectionFactory = $productCollectionFactory;
parent::__construct($context, $data);
}
public function getProductCollection()
{
$productCollection = $this->_productCollectionFactory->create();
$productCollection->setPageSize(3);
foreach ($productCollectionas $product)
{
echo '<pre>';
print_r($product->getData());
}
return $collection;
}
}
Now, we will look how to get product collection in different ways/situations.
Get product collection with specific product attribute/field.
$productCollection = $this->_productCollectionFactory->create();
$productCollection->addAttributeToSelect(['sku','name','price']);
$productCollection->setPageSize(10);
return $productCollection;
Get product collection with all product attribute/fields.
$productCollection = $this->_productCollectionFactory->create();
$productCollection->addAttributeToSelect('*');
$productCollection->setPageSize(10);
return $productCollection;
Get product collection with multiple categories.
$categories = [10,22,35]; // ID of categories
$productCollection = $this->_productCollectionFactory->create();
$productCollection->addAttributeToSelect('*');
$productCollection->addCategoriesFilter(['in' => $categories]);
return $productCollection;
Get product collection with specific category.
$categoryId = '900';
$category = $this->categoryFactory->create()->load($categoryId);
$productCollection= $this->_productCollectionFactory->create();
$productCollection->addAttributeToSelect('*');
$productCollection->addCategoryFilter($category);
$productCollection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
$productCollection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
return $productCollection;
Get product collection with product type.
$productCollection= $this->_productCollectionFactory->create();
$productCollection->addAttributeToSelect('*');
$productCollection->addAttributeToFilter('type_id', ['eq' => 'simple']);
$productCollection->getSelect()->order('created_at', \Magento\Framework\DB\Select::SQL_DESC);
$collection->getSelect()->limit(10);
return $productCollection;
Get product collection with store_id OR website_id.
$store_Id = 1;
$website_Ids= [1,2];
$productCollection= $this->_productCollectionFactory->create();
$productCollection->addAttributeToSelect('*');
// Filtered by storeID
$productCollection->addStoreFilter($store_Id );
// Filtered by websiteID
$productCollection->addWebsiteFilter($website_Ids);
return $collection;
Filter Product Collection
You can filter product collection by adding condition same like MYSQL WHERE clause.
Is equal to: ‘eq’
$productCollection = $this->_productCollectionFactory->create();
$productCollection ->addAttributeToFilter('status', ['eq' => 1]);
Is not equal to: ‘neq’
$productCollection = $this->_productCollectionFactory->create();
$productCollection->addAttributeToFilter('status', ['neq' => 1]);
Greater than: ‘gt’
$productCollection = $this->_productCollectionFactory->create();
$productCollection->addAttributeToFilter('price', ['gt' => 100]);
Greater than equal to: ‘gteq’
$productCollection = $this->_productCollectionFactory->create();
$productCollection->addAttributeToFilter('price', ['gteq' => 1000]);
Less than: ‘lt’
$productCollection = $this->_productCollectionFactory->create();
$productCollection->addAttributeToFilter('price', ['lt' => 100]);
Less than equal to: ‘lteq’
$productCollection = $this->_productCollectionFactory->create();
$productCollection->addAttributeToFilter('price', ['lteq' => 100]);
Like: ‘like’
$productCollection = $this->_productCollectionFactory->create();
$productCollection->addAttributeToFilter('name', ['like' => '%MC00001%']);
Not Like: ‘nlike’
$productCollection = $this->_productCollectionFactory->create();
$productCollection->addAttributeToFilter('name', ['nlike' => '%MC00001%']);
In Array: ‘in’
$productCollection = $this->_productCollectionFactory->create();
$productCollection->addAttributeToFilter('entity_id', ['in' => [207,225]]);
Not in Array: ‘nin’
$productCollection = $this->_productCollectionFactory->create();
$productCollection->addAttributeToFilter('entity_id', ['nin' => [101,201]]);
NULL: ‘null’
$productCollection = $this->_productCollectionFactory->create();
$productCollection->addAttributeToFilter('description', ['null' => true]);
NOT NULL: ‘notnull’
$productCollection = $this->_productCollectionFactory->create();
$productCollection->addAttributeToFilter('name', ['notnull' => true]);
Sorting Collection: You can do all operations with any mgaento entity Collection like you have performed with MYSQL query.
Order By ASC | DESC
$productCollection = $this->_productCollectionFactory->create();
// set ascending order to collection
$productCollection->setOrder('name', 'ASC');
OR
$productCollection->getSelect->order('name', 'ASC');
// set descending order to collection
$productCollection->setOrder('name', 'DESC');
OR
$productCollection->getSelect->order('name', 'DESC');
Set LIMIT for Collection with pagination:
$productCollection = $this->_productCollectionFactory->create();
$productCollection->setPageSize(20)->setCurPage(1)->load();
Get random products from collection
$productCollection = $this->_productCollectionFactory->create();
$productCollection->setPageSize(5);
$productCollection->getSelect()->orderRand();
Get Collection count
$productCollection = $this->_productCollectionFactory->create();
// the collection is not loaded.
echo $productCollection->getSize();
// the count() will load whole collection
echo $productCollection->count();
Group by Collection
$productCollection = $this->_productCollectionFactory->create();
$productCollection->getSelect()->group('entity_id');
Print Collection Query:(for debug collection)
$productCollection= $this->_productCollectionFactory->create();
echo $productCollection->getSelect()->__toString();
we hope this artical is very helpful for you. please leave comment if you have any question. Thank You!