Magento 2 Product Collection Magento 2 Product Collection

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 = MagentoFrameworkAppObjectManager::getInstance();

$productCollectionFactory = $objectManager->get('MagentoCatalogModelResourceModelProductCollectionFactory');

$collection = $productCollectionFactory->create();
$collection->setPageSize(3);

foreach ($collection as $product) {
    print_r($product->getData());
}

Load product collection with all attribute

$productCollectionFactory = $objectManager->get('MagentoCatalogModelResourceModelProductCollectionFactory');

$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('MagentoCatalogModelResourceModelProductCollectionFactory');

$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('MagentoCatalogModelResourceModelProductCollectionFactory');

$categoryCollectionFactory = $objectManager->get('MagentoCatalogModelCategoryFactory');

$categoryId = '9';
$category = $categoryCollectionFactory->create()->load($categoryId);

$collection = $productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addCategoryFilter($category);
$collection->addAttributeToFilter('visibility', MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
$collection->addAttributeToFilter('status',MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);

foreach ($collection as $product) {
    print_r($product->getData());
}

Filter Product Collection

Is equal to

$productCollectionFactory = $objectManager->get('MagentoCatalogModelResourceModelProductCollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToFilter('status', array('eq' => 1));

Is not equal to

$productCollectionFactory = $objectManager->get('MagentoCatalogModelResourceModelProductCollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToFilter('status', array('neq' => 1));

Greater than

$productCollectionFactory = $objectManager->get('MagentoCatalogModelResourceModelProductCollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToFilter('price', array('gt' => 50));

Greater than or equal To

$productCollectionFactory = $objectManager->get('MagentoCatalogModelResourceModelProductCollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToFilter('price', array('gteq' => 50));

Less than

$productCollectionFactory = $objectManager->get('MagentoCatalogModelResourceModelProductCollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToFilter('price', array('lt' => 50));

Less than or equal To

$productCollectionFactory = $objectManager->get('MagentoCatalogModelResourceModelProductCollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToFilter('price', array('lteq' => 50));

Like

$productCollectionFactory = $objectManager->get('MagentoCatalogModelResourceModelProductCollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToFilter('sku', array('like' => '%ABC%'));

Not Like

$productCollectionFactory = $objectManager->get('MagentoCatalogModelResourceModelProductCollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToFilter('sku', array('nlike' => '%ABC%'));

In Array

$productCollectionFactory = $objectManager->get('MagentoCatalogModelResourceModelProductCollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToFilter('entity_id', array('in' => array(1,2,3)));

Not In Array

$productCollectionFactory = $objectManager->get('MagentoCatalogModelResourceModelProductCollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToFilter('entity_id', array('nin' => array(1,2,3)));

NULL

$productCollectionFactory = $objectManager->get('MagentoCatalogModelResourceModelProductCollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToFilter('description',  array('null' => true));

Not NULL

$productCollectionFactory = $objectManager->get('MagentoCatalogModelResourceModelProductCollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToFilter('description',  array('notnull' => true));

Sort Product Collection

Order by Ascending

$productCollectionFactory = $objectManager->get('MagentoCatalogModelResourceModelProductCollectionFactory');
$collection = $productCollectionFactory->create();
$collection->setOrder('price', 'ASC');

Order by Descending

$productCollectionFactory = $objectManager->get('MagentoCatalogModelResourceModelProductCollectionFactory');
$collection = $productCollectionFactory->create();
$collection->setOrder('price', 'DESC');

Limit Product Collection

Limit

$productCollectionFactory = $objectManager->get('MagentoCatalogModelResourceModelProductCollectionFactory');
$collection = $productCollectionFactory->create();
$collection->setPageSize(50)->load();

Limit with current page

$productCollectionFactory = $objectManager->get('MagentoCatalogModelResourceModelProductCollectionFactory');
$collection = $productCollectionFactory->create();
$collection->setPageSize(50)->setCurPage(2)->load();

Count Collection

Count

$productCollectionFactory = $objectManager->get('MagentoCatalogModelResourceModelProductCollectionFactory');
$collection = $productCollectionFactory->create();
echo $collection->count();
$productCollectionFactory = $objectManager->get('MagentoCatalogModelResourceModelProductCollectionFactory');
$collection = $productCollectionFactory->create();
echo $collection->getSelect()->__toString();

If you liked this post, then please like us on Facebook and follow us on Twitter.

Written by

Chirag

Chirag is a Founder & eCommerce Technology Leader at MageArmy. He leads the eCommerce development team and drives the company’s technology vision, helping businesses build, scale, and optimize high-performing online stores. With years of experience in eCommerce development, he has worked with businesses across industries to solve complex technical challenges and create scalable digital commerce solutions. He loves sharing practical insights about eCommerce development, Magento, Adobe Commerce, and the latest trends in digital commerce to help online businesses grow and succeed.