-1

I want all product of particular category on basis of category ID.

I have already tried below code but not solved:

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

[{"entity_id":"245","sku":"U001","type_id":"simple","category_id":null,"cat_index_position":"0","product_image":"\/p\/e\/peach-boy-boys-shirts_1_a8c05a1421e5dcf8ad5308398e0b3591_1.jpg","small_image":"\/p\/e\/peach-boy-boys-shirts_1_a8c05a1421e5dcf8ad5308398e0b3591_1.jpg","thumbnail":"\/p\/e\/peach-boy-boys-shirts_1_a8c05a1421e5dcf8ad5308398e0b3591_1.jpg","productName":"DY Andheri Peach School Uniform","price":"500.0000","isProductInStock":19,"isProductEndorsed":false,"option_collection":[]},{"entity_id":"250","sku":"U003","type_id":"configurable","category_id":null,"cat_index_position":"0","product_image":"\/0\/a\/0aea950b-1c0a-4adf-85ce-c521555462651534842479667-chalk-by-pantaloons-girls-dresses-4851534842479574-1.jpg","small_image":"\/0\/a\/0aea950b-1c0a-4adf-85ce-c521555462651534842479667-chalk-by-pantaloons-girls-dresses-4851534842479574-1.jpg","thumbnail":"\/0\/a\/0aea950b-1c0a-4adf-85ce-c521555462651534842479667-chalk-by-pantaloons-girls-dresses-4851534842479574-1.jpg","productName":"DY Andheri Girls School Uniform","price":"450.0000","isProductInStock":0,"isProductEndorsed":false,"option_collection":[{"sku":"DY Andheri Girls School Uniform-S","product_id":"250","attribute_id":"151","default_title":"Size","value_index":"30","display_label":"S","price":"450.0000"},{"sku":"DY Andheri Girls School Uniform-M","product_id":"250","attribute_id":"151","default_title":"Size","value_index":"31","display_label":"M","price":"450.0000"},{"sku":"DY Andheri Girls School Uniform-L","product_id":"250","attribute_id":"151","default_title":"Size","value_index":"32","display_label":"L","price":"450.0000"},{"sku":"DY Andheri Girls School Uniform-XL","product_id":"250","attribute_id":"151","default_title":"Size","value_index":"33","display_label":"XL","price":"450.0000"}]}]
Bsquare ℬℬ
  • 4,423
  • 11
  • 24
  • 44

1 Answers1

0

You can get specific category product collection by

        $categoryId = 3;
        $objectManager =  \Magento\Framework\App\ObjectManager::getInstance(); 
        $categoryFactory = $objectManager->create('Magento\Catalog\Model\CategoryFactory');
        $category = $categoryFactory->create()->load($categoryId);

        $productCollectionFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
        $collection = $productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->addCategoryFilter($category);
        $collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
        $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);

You can replace categoryId with your category Id and collection will return all product which is enabled and visibility is set to catalog search and that belong to specify category

Nidhi
  • 141
  • 1
  • 7
  • Its Shows ProductCollection for category Inside Root Category Not for Outside Root category, my category placed outside of root – Suraj Gholap Feb 05 '19 at 05:25