Tutorialsplane

Magento 2 get store id by store code


Magento 2 get store id by store code– We can use class \Magento\Store\Model\StoreManagerInterface to get the store id by store code. Here in this article we are going to explain how you can get store id by store code using object manager.


Magento 2 get store id by store code Example

You can use \Magento\Store\Model\StoreManagerInterface to get the store id from store code simply as below-

Example:

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();    
$storeManager = $objectManager->create("\Magento\Store\Model\StoreManagerInterface");
$storecode = 'my_store_1';
    $allStores = $storeManager->getStores(true, false);
    foreach($allStores as $store){
    if($store->getCode() === $storecode){
        $storeId = $store->getId();
    }
   }
echo $storeId;

$storeManager->getStores return all stores so you have to match your store code and get store id. On the same way you can get other details about the store.