Magento – How to get root category using store id in magento 2 block file

blocksmagento2store-id

How to get root category using store id.
we get store id using $storeId = $this->getStoreId();
We use below code for get root category using store id; but we get wrong.

$rootCat = $this->_storeManager->getStore($storeId)->getRootCategoryId();

Any one help us for solve this.

Best Answer

You can use \Magento\Store\Model\StoreManagerInterface class to get store default root category Id.

<?php
class Test
{
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManagerInterface;

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



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

}
Related Topic