How To Get Default Store ID in Magento 2
In Magento 2, it is possible creating a website with multiple stores having different designs, products, purposes, etc. Store view is nothing but, a variation of the same store with different attributes.
For example, in the case of different geographical regions, the attributes such as product title and description have to be translated into different languages. Thus, the store owner needs to create multiple store views with multiple languages.
However, every Magento 2 store has a default store view. The store owner may require to get the default store ID in Magento 2 programmatically for various purposes. For example, if you want to show the default Magento store view in a drop-down menu to the customers, you may require to retrieve the default store view of that particular Magento website.
You can refer to the solution provided here to get the default store ID in Magento 2.
Method to Get the Default Store ID in Magento 2
1 2 3 4 5 6 7 8 9 |
use Magento\Store\Model\StoreManagerInterface; protected $storeManager; public function __construct(StoreManagerInterface $storeManager) { $this->storeManager = $storeManager; } $websiteId =1; // get default store id of website 1 $storeId = $this->storeManager->getWebsite($websiteId)->getDefaultStore()->getId(); |
That’s it.
If you have any questions regarding this post, feel free to ask in the comments section below
I’d be happy to answer.
Do consider sharing this post with the Magento community via social media.
Thank You.
Jignesh Parmar
An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.
Prev
Magento Year in Review 2021
Magento 2 WhatsApp Order Notification 101: A Complete Guide
Next