0

How to get product by parent-category-id and sub-category-id?

Magento 2.0

1

Use below code

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$categoryFectory = $objectManager->create('Magento\Catalog\Model\CategoryFactory');
$productCollection = $categoryFectory->load('category_id')->getProductCollection()->addAttributeToSelect('*');
  • I want to product collection sub-category also. – Masud Shaikh Sep 5 '18 at 9:59
  • Now, I have got single category product collection. But i want sub-category product collection also. – Masud Shaikh Sep 5 '18 at 10:00
  • make parent category is_anchor=yes then it will fetch subcategory products also on the same – Ansar Husain Sep 5 '18 at 10:03
  • can you tell me where add this code? category is_anchor=yes – Masud Shaikh Sep 5 '18 at 10:08
  • no no not in code just do it from admin category section – Ansar Husain Sep 5 '18 at 10:08
1

Please check this following link for get more idea about it.

Click here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.