cancel
Showing results for 
Search instead for 
Did you mean: 

I need to get product collection by child category in magento 2

I need to get product collection by child category in magento 2

I need to get product collection by child category.

Category Structure:

 

Shop  
  Colors 
    Rushmore -- 12 
    Boston Mill 
    Little Cottonwood

Here is my code:

$colorId = 12 (This is my rushmore id(child id))    
$categoryFactory
= $objectManager->get('\Magento\Catalog\Model\CategoryFactory');
$cat = $categoryFactory->create()->load($colorId);

$categoryProducts = $cat->getProductCollection()->addAttributeToSelect('*')->addCategoryFilter($cat);
foreach
($categoryProducts as $product){
print_r
($product->getData()); }

 

5 REPLIES 5

Re: I need to get product collection by child category in magento 2

Hi @Shiwani Miglani 

Please try once following code:

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

Re: I need to get product collection by child category in magento 2

Hi @Shiwani Miglani 
There may be area specific error.

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        
 
$appState = $objectManager->get('\Magento\Framework\App\State');
$appState->setAreaCode('frontend');
 
$categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');
$categoryHelper = $objectManager->get('\Magento\Catalog\Helper\Category');
$categoryRepository = $objectManager->get('\Magento\Catalog\Model\CategoryRepository');
 
$categoryId = 21; // YOUR CATEGORY ID
$category = $categoryFactory->create()->load($categoryId);
 
$categoryProducts = $category->getProductCollection()
                             ->addAttributeToSelect('*');
                             
foreach ($categoryProducts as $product) {
    //print_r($product->getData());
    // printing category name and url
    echo $product->getName() . ' - ' . $product->getProductUrl() . '<br />';
}


I hope it will work for you!

Re: I need to get product collection by child category in magento 2

@Vimal Kumar 

 

This code is working for the parent category.

Re: I need to get product collection by child category in magento 2

@Shiwani Miglani 
Below code is working fine for main and child category both. 

<?php
    use Magento\Framework\App\Bootstrap;
    include('app/bootstrap.php');
    $bootstrap = Bootstrap::create(BP, $_SERVER);

    $objectManager = $bootstrap->getObjectManager();

    $state = $objectManager->get('Magento\Framework\App\State');
    $state->setAreaCode('frontend');

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

    echo "<pre>";print_r($allcategoryproduct->getData());



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

    echo "<pre>";print_r($allcategoryproduct->getData());


Please check once.

You can copy this code and create a new file on root and check in the browser.

I hope it will work for you.

Re: I need to get product collection by child category in magento 2

Hi @Shiwani Miglani ,

Please check the below code and its tested and working fine.

$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$categoryCollectionFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');
$categoryId = '5'; //put your category id here
$category = $categoryCollectionFactory->create()->load($categoryId);
$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);
foreach ($collection as $product) {
print_r($product->getData());
}

If it works for you then please accept the solution and give Kudos.

Thanks