Magento 2 – Product Collection
In day to day life of Magento 2 developer, somewhere in each and every project, a developer needs a product collection. With that regards, In this code snippet post, we will see all the aspect of product collection such as collection, filter, sorting etc.
Note: For the demonstrated purpose we have used Objectmanager.Codextblog never recommend the direct use of ObjectManager.One should always use a constructor method to instant an object.
Product Collection
Load product collection
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollectionFactory->create(); $collection->setPageSize(3); foreach ($collection as $product) { print_r($product->getData()); }
Load product collection with all attribute
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollectionFactory->create(); $collection->addAttributeToSelect('*'); $collection->setPageSize(3); foreach ($collection as $product) { print_r($product->getData()); }
Load product collection with specific attribute
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollectionFactory->create(); $collection->addAttributeToSelect('name'); $collection->addAttributeToSelect('price'); $collection->setPageSize(3); foreach ($collection as $product) { print_r($product->getData()); }
Load product collection by category
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $categoryCollectionFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory'); $categoryId = '9'; $category = $categoryCollectionFactory->create()->load($categoryId); $collection = $productCollectionFactory->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
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollectionFactory->create(); $collection->addAttributeToFilter('status', array('eq' => 1));
Is not equal to
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollectionFactory->create(); $collection->addAttributeToFilter('status', array('neq' => 1));
Greater than
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollectionFactory->create(); $collection->addAttributeToFilter('price', array('gt' => 50));
Greater than or equal To
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollectionFactory->create(); $collection->addAttributeToFilter('price', array('gteq' => 50));
Less than
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollectionFactory->create(); $collection->addAttributeToFilter('price', array('lt' => 50));
Less than or equal To
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollectionFactory->create(); $collection->addAttributeToFilter('price', array('lteq' => 50));
Like
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollectionFactory->create(); $collection->addAttributeToFilter('sku', array('like' => '%ABC%'));
Not Like
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollectionFactory->create(); $collection->addAttributeToFilter('sku', array('nlike' => '%ABC%'));
In Array
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollectionFactory->create(); $collection->addAttributeToFilter('entity_id', array('in' => array(1,2,3)));
Not In Array
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollectionFactory->create(); $collection->addAttributeToFilter('entity_id', array('nin' => array(1,2,3)));
NULL
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollectionFactory->create(); $collection->addAttributeToFilter('description', array('null' => true));
Not NULL
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollectionFactory->create(); $collection->addAttributeToFilter('description', array('notnull' => true));
Sort Product Collection
Order by Ascending
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollectionFactory->create(); $collection->setOrder('price', 'ASC');
Order by Descending
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollectionFactory->create(); $collection->setOrder('price', 'DESC');
Limit Product Collection
Limit
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollectionFactory->create(); $collection->setPageSize(50)->load();
Limit with current page
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollectionFactory->create(); $collection->setPageSize(50)->setCurPage(2)->load();
Count Collection
Count
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollectionFactory->create(); echo $collection->count();
print Collection Query
Print query
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollectionFactory->create(); echo $collection->getSelect()->__toString();
If you liked this post, then please like us on Facebook and follow us on Twitter.
Leave a Comment
(0 Comments)
Useful Magento 2 Articles
Author Info
Chirag
Connect With Me