How to get a store in Magento 2 programmatically?
In the article below we shed light on how to get a current store in Magento 2 programmatically. The following information is useful when you create a PHP script or Magento 2 module. Sometimes, it is necessary to check, which store is currently active or for which store you need to get or change values. The procedure is quite straightforward: you need only a few lines of code to do that. More tips from Magento 2 cookbook.
Table of contents
1) How to get a store in Magento 2 programmatically
In Magento 1, you can get a store and the current store programmatically quite simple:
1 |
Mage::app()->getStore() |
As for Magento 2, the procedure looks as follows:
1 2 3 4 5 6 7 8 9 10 11 12 |
/** * @param int|string|null|bool|\Magento\Store\Api\Data\StoreInterface $id [optional] * @return \Magento\Store\Api\Data\StoreInterface * @throws \Magento\Framework\Exception\NoSuchEntityException */ function rm_store($id = null) { /** @var \Magento\Framework\ObjectManagerInterface $om */ $om = \Magento\Framework\App\ObjectManager::getInstance(); /** @var \Magento\Store\Model\StoreManagerInterface $manager */ $manager = $om->get('Magento\Store\Model\StoreManagerInterface'); return $manager->getStore($id); } |
2) How to get a store in Magento 2 programmatically
Let’s explore how to get a current store in Magento 2 with every class that injects StoreManagerInterface. You can receive all the necessary store-related data from a current store manager. However, an object differs for each store frontend even if you call it from the backend. With the help of the following code, you can create a simple class that injects this store manager interface:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php namespace MyCompany\MyModule\Block; class MyModel extends \Magento\Framework\View\Element\Template { 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(); } } |
The getStoreId() method returns the ID from the current active Magento 2 store.
Different scope
Note that you can’t use isAdmin() to check if this code was called from the adminhtml context. While it was a common option in Magento 1, 2x has its algorithm. To check this information, use a method based on the \Magento\Framework\App\State class. First of all, you need to inject it. Next, call getAreaCode. You will get information on whether this class was called from the adminhtml of frontend or not. Below, you can see an example of a simple if statement for this case:
1 2 3 |
if ($this->state->getAreaCode() == \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE) { // Todo for adminhtml case } |
It checks if the area code equals adminhtml, which is defined as follows in \Magento\Backend\App\Area\FrontNameResolver:
1 |
const AREA_CODE = 'adminhtml'; |
Conclusion
As you can see, the procedure of getting a current store in Magento 2 differs from the one common to Magento 2. However, it is not difficult at all. For further information, follow these links: