Jigar KarangiyaJigar Karangiya

Magento 2 – How to get current Store ID Programmatically?

JK
Jigar Karangiya
· 1 min read
Magento 2 – How to get current Store ID Programmatically?

Hello Devs,

During development, You may need to get some store-related information programmatically.

We can fetch information such as

  • Get Current Store
  • Get Store Data of the current store
  • Get Current Store ID
  • Get another Store by specifying the store ID
  • Get the Current Store Name
  • Get Current Store Status
  • Get a Custom page URL / Build Store-Specific URLs
  • Get Current Website ID
  • Get Current URL

The above information can be fetched easily in Magento using Magento’s Magento\Store\Model\StoreManagerInterface class.

Let’s see the below example for different cases :

php
<?php
namespace Jigar\Tutorials\Block;
use Magento\Store\Model\StoreManagerInterface;
/**
 * Class for get current store related information
 */
class Example
{
    /**
     * @var StoreManagerInterface
     */
    private $storeManager;
    
    /**
     * @param StoreManagerInterface $storeManager
     */
    public function __construct(
        StoreManagerInterface $storeManager
    ) {
        $this->storeManager = $storeManager;
    }
    /**
     * {@inheritdoc}
     */
    public function execute()
    {
        /* Get Current Store */
       $currentStore = $this->storeManager->getStore();
       /* Get Store Data of current store */
       $store = $this->storeManager->getStore()->getData();
        
        /* Get Current Store ID */
       $storeId = $this->storeManager->getStore()->getId();
       /* Get another Store by store ID */
       $anotherStore = $this->storeManager->getStore(3);
       /* Get Current Store Name */
       $storeName = $this->storeManager->getStore()->getName();
       /* Get Current Store Status */
       $isActive = $this->storeManager->getStore()->isActive();
       /* Get Custom page URL */
       $customUrl = $this->storeManager->getStore()->getUrl('contact');
       /* Get Current Website ID */
       $websiteId = $this->storeManager->getStore()->getWebsiteId();
       /* Get Current URL */
       $currentUrl = $this->storeManager->getStore()->getCurrentUrl();
    }
}

Isn’t it easy? See related blogs like this.

Magento 2: How to get all websites list programmatically?

Magento 2: How to get all stores list programmatically

Add Yes/No Attribute in Magento 2 programmatically

Thank you !!

Written by Jigar Karangiya, Adobe Commerce & Magento 2 developer.

More about me

Related posts