How to get Store ID by store code in Magento 2



Please use below code :

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();    
$storeManager = $objectManager->create("\Magento\Store\Model\StoreManagerInterface");
$storecode = 'de'; //storecode here
    // get array of stores with storecode as key
    $stores = $storeManager->getStores(true, true);
    // check stores array for this storecode 
        $store_id = $stores[$storecode]->getId();
echo $store_id;

You could use Magento\Store\Api\StoreRepositoryInterface.

In your constructor:

protected $storeRepository;

public function __construct(
    \Magento\Store\Api\StoreRepositoryInterface $storeRepository
) {
    $this->storeRepository= $storeRepository;

From where you would like to get the store ID:

try {
    $store = $this->storeRepository->get('store_code');
    echo $store->getId(); // this is the store ID
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
    // store not found

We can use below code to get store id by using store code.

$store = $this->_storeManager->load($storeCode);
   $storeId = $store->getId();


$storeCode = 'malaysia';
AND $this->_storeManager is instance of Magento\Store\Model\Store

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.