Tutorialsplane

Magento 2 get product collection by category id


Magento 2 get product collection by category id We can get category products by category id using \Magento\Catalog\Model\CategoryFactory object. Here in this article, we are going to explain how you can get product collection of any category using object manager.


Magento 2 get product collection by category id Example

You can get category product collection simply as below-

Example:

$categoryId = 999;
$objectManager =   \Magento\Framework\App\ObjectManager::getInstance();
$catProductCollection = $objectManager->create('\Magento\Catalog\Model\CategoryFactory')->load($categoryId)->getProductCollection()->addAttributeToSelect('*');

foreach($catProductCollection as $product){
  echo $product->getName();
  echo "<br/>";
  echo $product->getSku();

}