🔥 Just Launched! Werra Premium Template for HyväSee it in Action

How to Programmatically Get Root Category ID in Magento 2

By Jignesh ParmarUpdated on May 21, 2025 1 min read

Magento 2 offers several flexible and customization options to provide the customers with a comfortable shopping platform. This robust and highly flexible eCommerce platform is suited for business of all size.

It is a highly customizable eCommerce platform that can create storefronts as per the seller’s requirements. You can sell multiple products from multiple niches in Magento by dividing them into different categories.

In Magento 2, you can create root categories to categorize the products in your store to manage them efficiently. You can also assign each of the root categories to separate storeviews to sell different category products on different storeviews.

Sometimes, you may need to programmatically get root category ID in Magento 2. In this post, I have shared complete solution on how you can get the root category in Magento 2.

Method to Programmatically Get Root Category ID in Magento 2:

You can get the root category ID in Magento 2 by calling the getRootCateogryId() function in the phtml file.

use Magento\Store\Model\StoreManagerInterface;
 
protected $storeManager;
 
public function __construct(
 StoreManagerInterface $storeManager)
{
 $this->storeManager = $storeManager;
}
 
 
$websiteId =1;
 
$storeId = $this->storeManager->getWebsite($websiteId)->getDefaultStore()->getId();
$rootNodeId = $this->storeManager->getStore($storeId)->getRootCategoryId();

That’s it!

Related Read: How to Programmatically Get All Websites in Magento 2

Do remember to share the solution with the Magento community via social media.

Thank You.

Jignesh Parmar Full Image
Article byJignesh Parmar

An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.