How to Programmatically Get Root Category ID in Magento 2
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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!
Any doubts can be mentioned in the comment section below. I’d glad to help you out.
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
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.
Prev
How to Match Customer Password in Magento 2
How to Add Quick View Popup in Magento 2
Next