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(); } |
Advertisements
Add Comment
📖 Read More
- 1. Magento 2 get store id by store code
- 2. Magento 2 get product collection filter by Attribute
- 3. Magneto 2 Add product to cart programmatically
- 4. Magento 2 Override Controller