How to Get Store I'd from Store Code in Magento 2

Magento is the best suitable platform for online eCommerce store owners. In addition, Magento provides many functionalities that make the daily work of eCommerce merchants easy. However, Magento also provides multi-store functionality for its users. But it can be difficult when an admin needs to work with multi-stores. While working with multi-store, the main need is to get the current store id from the store code in Magento 2. However, this article is about how to get store id from store code in Magento 2. So let's get started.

Upgrade your hosting server to Nexcess or Hostinger. They are the most popular ones and provide the best service above all.

How to get store id from store code in Magento 2

So to manage the multi-store, you will need to get the current store ID in your Magento 2. In most cases, when any customer asks you for customization, you first need to find their store. And to find the store, you first have to get the store ID. Therefore, we will see how to get the store id from the store code in Magento 2.

1. Using Block function

First, you need to implement the below method to get the current store ID.

protected $storeManager;
 
public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    array $data = []
)
{
    $this->storeManager = $storeManager;
    parent::__construct($context, $data);
}
public function getStoreId()
{
    return $this->storeManager->getStore()->getId();
}

Now you have to call this block function in your phtml file to get the current store ID.

echo $block->getStoreId();

2. Using Object Manager

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        
 
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
 
echo $storeManager->getStore()->getStoreId();

So, that's it.

Once you will get the store ID you can implement the logic as you need.

Quick Links:

How to get base URL in Magento 2.

How to get a free demo in Magento 2 extension

Final Words

Hope this article How to get store id from store code in Magento 2 will help you to find the store id. If you still have any queries or questions regarding this feel free to contact us. We will be more than happy to help you.

If you are looking for the best email service provider then, Sendinblue and GetResponse can be good choices.

Query solved?! If still facing the issue, write in the comment or contact us.
Get in touch with us to hire Magento Developer dedicatedly. Also, we provide hourly-based service for any of your Magento development needs.

Happy Coding:-)