6

How to get Store ID by store code in Magento 2

Thanks

4 Answers 4

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;
1
  • Is the object manager recommend to use, is any best way to achieve this?
    – zus
    Oct 7, 2021 at 6:36
16

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
}
2
  • Way better than given answer. Single store is loaded instead of all of them. Also try/catch is used
    – Macas
    Nov 28, 2019 at 14:20
  • I had to use \Magento\Store\Model\StoreRepository instead.
    – Black
    Apr 1 at 13:39
3

Sometimes, during Magento 2 customization you need to get the store Information programmatically. You may need to get a current Store ID, Store Code, Name, Website ID, or current URL.

To retrieve this data use the singleton instance of the following class:

\Magento\Store\Model\StoreManagerInterface For example, you can include it in your class constructor and then call: You can define in separate function in helper and block and you can call it. I have just given the idea so it is all in execute function.

class Example{
  private $storeManager;
public function __construct(
    ...        
    \Magento\Store\Model\StoreManagerInterface $storeManager,        
    ...
) {
    ...
    $this->storeManager = $storeManager;        
    ...
}
/**
 * Examples
 */
public function execute()
{
   /* Get Current Store ID */
   $storeId = $this->storeManager->getStore()->getId();

   /* Get Current Store Code */
   $storeCode = $this->storeManager->getStore()->getCode();

   /* Get Current Store Name */
   $storeName = $this->storeManager->getStore()->getName();

   /* Get Current Store Status */
   $isActive = $this->storeManager->getStore()->isActive();

   /* Get Current Website ID */
   $websiteId = $this->storeManager->getStore()->getWebsiteId();

   /* Get Current Website Name */
   $websiteName = $this->storeManager->getStore()->getName();

   /* Get Current URL */
   $websiteId = $this->storeManager->getStore()->getCurrentUrl();

   /* Get Login Page URL */
   $loginPageUrl = $this->storeManager->getStore()->getUrl('customer/account/login');

   /* Get Code of Store With ID 2 */
   $secondStoreCode = $this->storeManager->getStore(2)->getCode();

    }
}

Hope this helps you.

1
  • This answer only explains how to retrieve the current Store, but the question was to retrieve a different store by code.
    – Jacques
    Feb 15 at 10:05
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();
    $store_name = '';
    // 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.

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.