3

How to get Store ID by store code in Magento 2

Thanks

7

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 
    if(isset($stores[$storecode]){
        $store_id = $stores[$storecode]->getId();
    }
echo $store_id;
2

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
}
-1

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

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

    Where 

$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.