magento-2-get-store-id

How to Get ID Store in Magento 2 [Full Tutorial]

by Trang Tran

Going with the rapid growth of the e-commerce market, having multi-stores tends to become a trend. However, store owners also face a significant challenge in managing a multi-store.

The store owners attempt to provide a store suitable for their customers’ native language, currency, etc. To do so, you must get the store ID before implementing any condition-based modification.

This guide will help you to do Magento 2 get current store ID. 

Magento 2 Get Store ID From Backend

A store ID, in general, is a unique number that distinguishes your store account from others. To find your store ID, please refer to the following guide:

Step 1: Go into the Magento 2 backend dashboard.

Step 2: Navigate to Content, choose Design, then Configuration.

magento-2-get-store-id

Step 3: Select the Store which requires the store ID and click “Edit”

magento-2-get-store-id

Step 4: The store ID is displayed in the URL.

magento-2-get-store-id

Remember that if the store ID is equal to one, do not display a popup of discounted items or create a store-specific CSV; otherwise, do so.

How To Get Store ID in Magento 2 Programmatically

Before going live, you must test or troubleshoot the functionality related to multiple stores after configuring. You must have the action of Magento 2 get store by id to expedite the process.

Several extensions for Magento 2 stores require the store ID during configuration. Thus, finding a specific store ID may take a Magento novice too long.

There are 2 methods to Magento 2 get all store IDs, including: 

  • Use the block function.
  • Use object manager.

However, both ways are required to add code to the template file. 

Way 1: Using the Block function

Firstly, to get store ID, you must use the procedure listed below.

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();
}

Next, to obtain the most recent store ID, you must now execute this block function in your phtml file.

echo $block->getStoreId();

Way 2: Using Object Manager

On the other hand, you can use the object manager to get store ID in Magento 2 from store code. Refer to this code below:

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

Tip To Using Store ID To Update Multiple Store View Pricing

Magento offers its users multi-store functionality. But it can be challenging when an admin has to deal with multiple stores.

The store owner must configure a single dashboard in Magento 2 to manage multiple stores. The requirement to obtain the current store ID in Magento 2 frequently arises when working in a multi-store setting.

You have several target markets for each store view with different shipping and storage costs. Therefore, customizing each store is a way to maximize business efficiency and save costs.

It is when you need to know store ID, mainly when customization is specific to the store. For instance, setting up the currency and prices in store A differ from those in store B.

However, if you feel the ways above are two complicated, we have another solution for updating tier price without Magento 2 get current store id.

Magento 2 Multiple Store View Pricing can help you update tier price based on multiple follow-up variables such as SKU, Customer Group ID, Price, and Store ID. With Restful API & GraphQL API add-on, updating, deleting price, and getting product list are in a heart-beat.

Not only providing the ability to update prices depending on the store, but this module is also a complete solution regarding multi-store management. Let’s quickly look at its highlight features:

  • Firstly, set up different pricing, special prices, and tier prices for each store view.
  • Secondly, easily configure the base currency for each Magento 2 store view. 
  • Thirdly, customers conveniently pay in the base currency of their chosen store.
  • Lastly, configure various promotional prices for a product in the Magento 2 store view.
magento-2-store-view

>>> INSTALL Magento 2 Multiple Store View Pricing to manage multi-store better!

Conclusion

Hopefully, after this post, you can clearly know how to get Magento 2 store ID and its benefits. This is essential when you manage a multi-store due to requiring different prices for each store view.

Luckily, if you don’t have coding experience, there is also an alternative way, Magento 2 Multiple Store View Pricing. It can help you change prices based on store ID without getting store ID from store code. 

BSS Commerce is one of the leading Multi-platform eCommerce solutions and web development services providers worldwide. With experienced and certified developers, we commit to bringing high-quality products and services to optimize your business effectively.

CONTACT NOW to let us know your problems. We are willing to support you every time.

Next Reading Suggestions

© 2019 BSS Commerce owned by THANH CONG INTER ., JSC. All Rights Reserved.
Business registration certificate no. 0106064469 issued by Hanoi Department of Planning and Investment on 19 December 2019.
Legal Representative: Mr. Nguyen Quang Trung.