How to Get Current Store ID in Magento 2
E-commerce has changed the way people do business and shop. With online shopping, a customer from France can make a purchase from a store in India! However, as the customer base increases, the store owners try to offer a shopping platform that is compatible with their shoppers’ local language, currency, etc.
Hence, Magento 2 allows having multi-stores to serve your customers with a shopping platform that is comfortable to them.
In order to work with more than one store from a single administration, the store owner has to set up multi store in Magento 2. While working with a multi-store environment, the need often arrives to get current store id in Magento 2.
Especially, when the store owner needs customization based on the store. For example, store A has different currency and product rates than store B.
Before implementing any such condition-based customization, you first have to get the store ID, and then implement the logic such as if the store id is equal to one, then don’t show a popup of discounted products or generate a store-specific CSV, otherwise do.
Check out the below code to do so using block and object manager.
Related Post: How to Get Website ID and Store ID from Admin in Magento 2
Method to Get Current Store ID in Magento 2
- Using Block
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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(); } |
1 |
echo $block->getStoreId(); |
- Using Object Manager
1 2 3 4 5 |
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface'); echo $storeManager->getStore()->getStoreId(); |
If you have questions regarding this post, feel free to ask in the comment section below.
I would be happy to answer.
Do consider sharing this post with Magento Community via social media.
Thank you.
Still need help? Hire our Adobe-certified Magento experts.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
2 Comments
how can i find store view id ?
i found 25+ links , everywhere i found echo $storeManager->getStore()->getStoreId(); but this is not giving me the store_view_id (scope_id)
Hello Smit,
Please be informed that the store ID and store view ID both are the same thing.
Thank You