7

How to get Store ID by store code in Magento 2

Thanks

5 Answers 5

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

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
}
3
  • Way better than given answer. Single store is loaded instead of all of them. Also try/catch is used
    – Macas
    Commented Nov 28, 2019 at 14:20
  • I had to use \Magento\Store\Model\StoreRepository instead.
    – Black
    Commented Apr 1, 2022 at 13:39
  • 1
    I know it's out of scope of the OP, but you can also call ->getById(2) if you know the store ID and not the code. This answer lead me in the right direction to solve my needs that way. There also is \Magento\Store\Api\WebsiteRepositoryInterface that is similar but for the website scope.
    – PromInc
    Commented Jan 18, 2023 at 19:45
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.

3
  • This answer only explains how to retrieve the current Store, but the question was to retrieve a different store by code.
    – Jacques
    Commented Feb 15, 2022 at 10:05
  • This answer is not solving the problem
    – Ashish Raj
    Commented Feb 14 at 8:49
  • I am just adding the get Stores all current settings. if you want store code to there id please use $storeId = (int)$this->storeRepository->get($storeCode)->getId(); I am putting this comment when we started the stack :). Commented Mar 4 at 11:09
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.

0

You can StoreRepository to get Store Id by Store code.

/**
 * @var \Magento\Store\Api\StoreRepositoryInterface 
 */
protected $storeRepository;

/**
 * constructor.
 * @param \Magento\Store\Api\StoreRepositoryInterface $storeRepository
 */
public function __construct(
    \Magento\Store\Api\StoreRepositoryInterface $storeRepository
) {
    $this->storeRepository= $storeRepository;
}

Now you can use it to get store id.

$storeCode = "default";
$storeId = (int)$this->storeRepository->get($storeCode)->getId();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.