How to get Store ID by store code in Magento 2
Thanks
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;
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
}
->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.
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.
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.
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();