Home » Magento, Magento 25 May 2016

Magento 2: Get Store Information (Store ID, Code, Name, URL, Website ID)

This article shows how we can get store information in Magento 2. We will be fetching store id, store code, store name, store url, and store’s website id. We also write a function which checks if the store is active or not.

Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of StoreManagerInterface in the constructor of my module’s block class.

app/code/Chapagain/HelloWorld/Block/HelloWorld.php

See more functions in vendor/magento/module-store/Model/Store.php.

Now, we print the store information in our template (.phtml) file.

Using Object Manager

Hope this helps. Thanks.

Magento 2

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • itsnotclear

    it’s not clear how to use this code.

  • riad lynx

    thank you very much

  • Jim

    I’m getting an error – Exception #0 (MagentoFrameworkExceptionLocalizedException): Invalid method MagentoFrameworkViewElementTemplate::isStoreActive

  • Robert Jung

    I’m getting also an error in Magento 2.1. during compilation:
    Incorrect dependency in class VendorModulBlockHelloWorld in Vendor/Modul/Block/HelloWorld.php
    MagentoStoreModelStoreManagerInterface already exists in context object

    can anyone help?

  • Hi Robert,

    We don’t need to inject MagentoStoreModelStoreManagerInterface in constructor because the parent class (MagentoFrameworkViewElementTemplate) already does that.

    I have updated the code. I hope the updated code will solve the error that you are getting during compilation.

  • Hi Jim,

    Please check if you have the function “isStoreActive()” in your block class.

    It should have the following code:

  • rajat sharma

    Hi, I am using same method by adding StoreManagerInterface to constructor. I am using store manager instance to retrieve website id. It is working fine for mostly, but in some cases It returns wrong website id. E.g user is logged into Website A, but Main website id is returned by store manager instance.

  • Robert Jung

    Thanks for your update now the compilation works, but I do not get the StoreId in my template. Do you have an idea what can this cause? At the Backend I see the extension is enabled. Big thanks for your help.

  • spc

    Hi, How can I add this block class to a different template? Specifically to the final_price.phtml template from Magento Catalog module. Thanks!