Pre Black Friday Sale. 20% OFF on all products. Coupon code: fme20off
Magento 2: How to Get Product Collection Filter by Category ID?
Magento offers a wide range of product management related functionality. One of these functionalities is the Magento 2 get product collection by category id. Although not a very frequently used feature, as a Magento developer, knowing it will come in handy at some point. This article discusses the get product collection by category id in Magento 2. Before doing so, let’s talk about why filtering by product ID can be useful.
Importance of Magento 2 Get Product by Category ID
1. Custom Category Pages
Suppose you need to create a new page that only displays products from categories A, B, and C. One option is to select each product manually. This won’t be a problem if you have very little products. But if you have 100s or 1000s of products, doing so manually can be frustrating and time consuming. By filtering products through category ID, you can easily set up a new page with the relevant products.
2. Marketing Purposes
Generic marketing campaigns serve no purpose. In today’s eCommerce market, only personalised and targeted marketing campaigns create valuable. Using the Magento 2 get product by category ID functionality, you can create marketing campaigns based on specific IDs.
3. Reporting and Analytics
You cannot run a successful eCommerce business without making informed decisions. By filtering products by category ID in Magento 2, you can easily create performance reports for specific product categories. This gives you detailed insight into how each category is performing and to improve sales performance.
Of course, there are countless other reasons why you must know this Magento 2 functionality. Nevertheless, the above 3 points do give you an idea about its importance. So here we are with the solution to Get Product Collection Filter by Category ID using Object Manager in Magento 2.
Get Product Collection from Single Category ID in Magento 2
The following is the example to get the list of products from one category with details like product name, url, price etc. in Magento 2 using Object Manager.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $categoryId = 10; $categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory')->create(); $category = $categoryFactory->load($categoryId); $products = $category->getProductCollection() ->addAttributeToSelect('*'); foreach ($products as $product) { echo $product->getId() . "<br />"; echo $product->getName() . "<br />"; echo $product->getProductUrl() . "<br />"; }
Get Product Collection from Multiple Category ID’s in Magento 2
If you want to get product collection from multiple category ids, apply the following code.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $ids = [7,8,9]; $collectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory')->create(); $products = $collectionFactory->addAttributeToSelect('*') ->addCategoriesFilter(['in' => $ids]); foreach ($products as $product) { echo $product->getId() . "<br />"; echo $product->getName() . "<br />"; echo $product->getProductUrl() . "<br />"; }
Final Thoughts on Get Product Collection by Category ID in Magento 2
This concludes our article on getting product by category ID in Magento 2. As you can see, the steps are quite easy. If you have any questions or need help, please don’t hesitate to contact our support team.
Related Articles:
This blog was created with FME's SEO-friendly blog