5

How to get Store ID by store code in Magento 2

Thanks

8

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;
| improve this answer | |
9

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
}
| improve this answer | |
  • Way better than given answer. Single store is loaded instead of all of them. Also try/catch is used – Macas Nov 28 '19 at 14:20
0
protected $_storeManager;

public function __construct(
    ......................
    .....................
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    ....................
    ....................
) {
    .....................
    ....................
    $this->_storeManager = $storeManager;
    ....................
    ...................
}   
public function execute()
{
    // get Currrent  store id
    $store = $this->_storeManager->getStore()->getId();

    // get Current Store Name
    $stores_list = $this->_storeManager->getStores(true, true);
    foreach ($stores_list as $storekey => $storevalue) {
        if ($store == $storevalue->getId()) {
            $store_name = $storevalue->getName();
        }
    }
    echo "Store Name".$store_name;
}

you can also get all Details of Store

I Hope This Helps You.

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

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.