Get Store ID by store code Magento 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
}
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;