How To

How to Get all the Child Categories IDs of the Specific Category in Magento 2?

Hello Magento Friends,

In today’s Magento 2 tutorial, we will learn How to Get all the Child Categories IDs of the Specific Category in Magento 2.

An online eCommerce store contains categories and subcategories to differentiate products. To perform any action on categories, the admin needs a category ID. In Magento 2, the child categories ID for a specific category can be retrieved using the following method.

So, without further ado, let’s get started.

Steps to Get all the Child Categories IDs of the Specific Category in Magento 2:

Say, you have the following categories in your Magento 2 store.

To get category IDs, follow the below step.

Step 1: Create a file in your Magento root directory at the below path

magento_root_directory\getcategory.php

Then add the code as follows.

<?php

use Magento\Framework\AppInterface;
ini_set('display_errors', TRUE);
try
{
    require_once __DIR__ . '/app/bootstrap.php';
}
catch (\Exception $e)
{
    echo 'Autoload error: ' . $e->getMessage();
    exit(1);
}

try
{
    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $appState = $objectManager->get('\Magento\Framework\App\State');
    $appState->setAreaCode('frontend');
    
    $categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');// Instance of Category Model
    $rootCategoryId = 2; // your root category Id
    $category = $categoryFactory->create()->load($rootCategoryId);
    $categoryIds= $category->getAllChildren(false);
    echo $categoryIds; //will return comma separated list of ids
}
catch (Exception $e)
{
    echo $e->getMessage();
}

?>

Output:

The above code will return the result as follows.

Conclusion:

Hopefully, you will be able to Get all the Child Categories IDs of the Specific Category in Magento 2. To apply customization on Magento 2 categories, you can Hire Magento Developer. Share the article with your friends, and stay tuned with us!

Happy Coding!

Click to rate this post!
[Total: 1 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

5 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

1 week ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

1 week ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago