I get the category by calling the method from the repository:

$category = $this->_categoryRepository->get($id);

But I have multiple Store views, one for each language. I saw that the get method takes the store ID as second parameter. The problem is I only have the store view code. How can I get the category details by store code?


You need to use Magento 2's dependency injection system. It means in your block class in __construct() method, inject Magento\Store\Model\StoreManagerInterface class as an object. Then you'll be able to get store view id. You need to do something like the following in your block class:

protected $storeManager;    

public function __construct(
    \Magento\Backend\Block\Template\Context $context,        
    \Magento\Store\Model\StoreManagerInterface $storeManager,        
    array $data = []
    $this->storeManager = $storeManager;        
    parent::__construct($context, $data);

 * Get store identifier
 * @return  int
public function getStoreId()
    return $this->storeManager->getStore()->getId();

If your current block class is Magento\Catalog\Block\Category\View, then you'd need to use di.xml to extend your own module's block class to this class. More information on extending classes in Magento 2 is here

| improve this answer | |

Please try this code-

$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface'); 
$storeid = $storeManager->getStore()->getStoreId();
$storecode = $storeManager->getStore()->getCode();
| improve this answer | |

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.