{"id":2008,"date":"2022-02-23T10:32:24","date_gmt":"2022-02-23T10:32:24","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/programmatically-get-root-category-id-in-magento-2\/"},"modified":"2025-05-21T17:01:08","modified_gmt":"2025-05-21T11:31:08","slug":"programmatically-get-root-category-id-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/programmatically-get-root-category-id-in-magento-2\/","title":{"rendered":"How to Programmatically Get Root Category ID in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is a highly customizable eCommerce platform that can create storefronts as per the seller\u2019s requirements. You can sell multiple products from multiple niches in Magento by dividing them into different categories.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In Magento 2, you can&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/create-new-root-category-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">create root categories<\/a>&nbsp;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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes, you may need to&nbsp;<em><strong>programmatically get root category ID in Magento 2<\/strong><\/em>. In this post, I have shared complete solution on how you can get the root category in Magento 2.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Method to Programmatically Get Root Category ID in Magento 2:<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can get the root category ID in Magento 2 by calling the getRootCateogryId() function in the phtml file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">use Magento\\Store\\Model\\StoreManagerInterface;\n \nprotected $storeManager;\n \npublic function __construct(\n StoreManagerInterface $storeManager)\n{\n $this->storeManager = $storeManager;\n}\n \n \n$websiteId =1;\n \n$storeId = $this->storeManager->getWebsite($websiteId)->getDefaultStore()->getId();\n$rootNodeId = $this->storeManager->getStore($storeId)->getRootCategoryId();<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Related Read:<\/strong>&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/programmatically-get-all-websites-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Programmatically Get All Websites in Magento 2<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do remember to share the solution with the Magento community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-2008","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2008","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=2008"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2008\/revisions"}],"predecessor-version":[{"id":13845,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2008\/revisions\/13845"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=2008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=2008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=2008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}