Magento 2 – get store information
This tutorial shows you how to get store information in Magento 2. If you are working on a multistore, then you need to do things in various websites, stores or store views. On important thing is to get information of each store an set the current used store to retrieve or update information.
Magento 2 – get store information
The base class for all store information is \Magento\Store\Model\StoreManagerInterface. With it you can extract all needed information. For this see the following code snippet:
1 2 3 4 5 | $storeManager = $objectManager->create("\Magento\Store\Model\StoreManagerInterface"); $stores = $storeManager->getStores(true, false); foreach($stores as $store){ echo $store->getId()." ".$store->getCode()."\n"; } |
This code shows how to get an instance of Magento 2 StoreManager and how to get all stores and some information about it. The output of that code may look like this:
You can see, that all stores have a unique id, where 0 is on every system admin, 1 will be the first defined store – normally the local language. Store 0 is always admin, the code of each individual store may be like en_en, language and localization (en_us and en_gb may be different stores).
How to get store manager
You may get store manager by object manager as I have shown here. A far better solution is to inject it to your model class. For this you need to define a __construct method and inject it:
1 2 3 4 5 6 7 8 9 10 | protected $_storeManager; public function __construct( ... \Magento\Store\Model\StoreManagerInterface $storeManager, ...) { $this->_storeManager = $storeManager; ... } |
You can then use your class variable storeManager to get needed information.
Information available
You get a store object by calling:
1 | $store = $this->_storeManager->getStore(); |
The following list shows you how to get needed information from a store object:
- $store->getId()
gets the internal id of that store. 0 is admin store. - $store->getWebsiteId()
get the id of the website this store belongs to. - $store->getCode()
this is the code defined for that store. Normally you set country or language codes. - $store->getName()
this is the name of the store. This text is only visible in backend. - $store->getCurrentUrl(true)
this is the url for that store. - $store->isActive()
a simple value 0 or 1 if store is active or not.
You may use the following method to set the current store for further lines of code:
1 | $this->storeManager->setCurrentStore($storeCode); |
Conclusion
I showed how to use store manager to get store information in Magento 2. A store object contain information about all stores defined in your backend.
once you set current store, how do you get the store id that is currently set?
thank you for that question, this is not mentioned in my post… it is quite easy to get current store id, just use:
Can you tell , How to get StoreName in js MAgento 2 ?!
To pass data from you PHP Classes to JS you can use ConfigProvider (class ConfigProvider implements ConfigProviderInterface)