Get store information (Store ID, Code, Name) in Magento 2

Before launching an online store, it is essential to get all information of your magento 2 store including current store ID, store code, store name, store url, store phone number, store email address and store website. This information can be used in different places such as your invoices, shipments, credit memos, or in communications between you and your customers. Providing detailed information also helps build trust and increase conversion rates. Moreover, fetching these data will make you easy to manage when you have more than one Magento 2 store in your arms, or in case of any updates in the future. In this tutorial article, the code snippets are given to allow inserting into the console admin.


Magento 2 extensions

Magento 2 extensions

Allow you to achieve more with your online store

Check it out!


Overview of getting store information in Magento 2

Step 1: Declare in Mageplaza_HelloWorld

You will use a block class of the module Mageplaza_HelloWorld, then possibly inject the object of StoreManagerInterface in the constructor of the module’s block class.

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

<?php
namespace Mageplaza\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
    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);
    }
    
    /**
     * Get store identifier
     *
     * @return  int
     */
    public function getStoreId()
    {
        return $this->_storeManager->getStore()->getId();
    }
    
    /**
     * Get website identifier
     *
     * @return string|int|null
     */
    public function getWebsiteId()
    {
        return $this->_storeManager->getStore()->getWebsiteId();
    }
    
    /**
     * Get Store code
     *
     * @return string
     */
    public function getStoreCode()
    {
        return $this->_storeManager->getStore()->getCode();
    }
    
    /**
     * Get Store name
     *
     * @return string
     */
    public function getStoreName()
    {
        return $this->_storeManager->getStore()->getName();
    }
    
    /**
     * Get current url for store
     *
     * @param bool|string $fromStore Include/Exclude from_store parameter from URL
     * @return string     
     */
    public function getStoreUrl($fromStore = true)
    {
        return $this->_storeManager->getStore()->getCurrentUrl($fromStore);
    }
    
    /**
     * Check if store is active
     *
     * @return boolean
     */
    public function isStoreActive()
    {
        return $this->_storeManager->getStore()->isActive();
    }
}
?>

Step 2: Get store information in .phtml file

Using the following script in the .phtml file and print the store information.

echo $block->getStoreId() . '<br />';
echo $block->getStoreCode() . '<br />';
echo $block->getWebsiteId() . '<br />';
echo $block->getStoreName() . '<br />';
echo $block->getStoreUrl() . '<br />';
echo $block->isStoreActive() . '<br />';

Now you have got all the store information you need. I hope this guide is helpful for your business management and conversion boost. If you have any queries about the article or any questions in general, don’t hesitate to use the comment section below! Also, check these related topics for your next steps:

Related Topics

Image Description
With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.

People also searched for

  • magento 2 get store information
  • get store information in magento 2
  • how to get store information
  • magento 2 get store id
  • magento 2 get current store id
  • get store id magento 2
  • magento 2 get current store
  • magento 2 get store code
  • get current store id in magento 2
  • magento 2 get current store code
  • get store code in magento 2
  • get current store id magento 2
  • magento2 get store id
  • how to get store id in magento 2
  • magento 2 get store by id
  • how to get current store id in magento 2
  • magento2 get current store id
  • magento 2 get store
  • get current store magento 2
  • get current store code magento 2
  • magento 2 store id
  • magento2 get current store
  • store manager magento 2
  • magento 2 get website id
  • magento 2 get store code in phtml
  • get store id in magento 2
  • magento 2 get store name
  • magento 2 get store information
  • magento 2 get store url
  • magento 2 get current website id
  • magento 2 get store by code
  • magento 2 get website code
  • magento 2 get product store id
  • 2.3.x, 2.4.x
x