2

How can i get root category id w.r.t store in header.phtml?

3 Answers 3

0

You can try the following code to your phtml file:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->create('Magento\Store\Model\StoreManagerInterface');
$rootCategoryId = $storeManager->getStore($storeManager->getStore()->getId())->getRootCategoryId();
1

You can override Header.php file and add construct function like -

protected $storeManagerInterface;

    public function __construct(\Magento\Store\Model\StoreManagerInterface $StoreManagerInterface)
    {
         $this->storeManagerInterface = $StoreManagerInterface;
    }

public function getRootCategoryId()
    {
        $storeID       = $this->storeManagerInterface->getStore()->getId();
        return $this->storeManagerInterface->getStore($storeID)->getRootCategoryId();
    }

Then in header.phtml use like

echo $block->getRootCategoryId();
0

Use \Magento\Store\Model\StoreManagerInterface class to get store default root category Id.

<?php 
class Test{

protected $storeManagerInterface;

public function __construct(\Magento\Store\Model\StoreManagerInterface $StoreManagerInterface)
{
     $this->storeManagerInterface = $StoreManagerInterface;
}



public function getRootCategoryId()
{
    $store = 1;
    return $this->storeManagerInterface->getStore($store)->getRootCategoryId();
}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.