Get Store Information in Magento 2

While developing a Magento 2 website, many time we might need to get the store information like store id, store code, store name, store URL, and store website. Fetching these store information is very important when we have more than one Magento 2 store. In this blog, I am going to show you how to get the store information (i.e, store id, store code, store name, store URL, and store website) in Magento 2.

In the below example, I will be using both Dependency Injection (DI) and Object Manager to get the store information (i.e, store id, store code, store name, store URL, and store website) in Magento 2.

Using Dependency Injection (DI)

Here is the example code to get the store information (i.e, store id, store code, store name, store URL, and store website) in Magento 2 using dependency injection.

In this, we might need to inject the object of StoreManagerInterface class in the constructor of our module’s block class and access it from the view ( .phtml ) file.

Sample File Path: app/code/YourCompanyName/YourModuleName/Block/YourCustomBlock.php

<?php
namespace YourCompanyName\YourModuleName\Block;
class YourCustomBlock extends \Magento\Framework\View\Element\Template
{
    /** 
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        array $data = []
    ) {
        $this->_storeManager = $storeManager;
        parent::__construct($context, $data);
    }
 
    /**
     * Get store id
     *
     * @return int
     */
    public function getStoreId() {
        return $this->_storeManager->getStore()->getId();
    }
 
    /**
     * 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();
    }

    /**
     * Get website id
     *
     * @return string|int|null
     */
    public function getWebsiteId() {
        return $this->_storeManager->getStore()->getWebsiteId();
    }
}

You can find more functions in vendor/magento/module-store/Model/Store.php.

Now, we can use the functions in our view (.phtml) file as follows.

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

Using Object Manager

Here is the example code to get the store information (i.e, store id, store code, store name, store URL, and store website) in Magento 2 using object manager.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');

echo $storeManager->getStore()->getStoreId() . '<br />'; 
echo $storeManager->getStore()->getCode() . '<br />'; 
echo $storeManager->getStore()->getName() . '<br />'; 
echo $storeManager->getStore()->getStoreUrl() . '<br />';
echo $storeManager->getStore()->getWebsiteId() . '<br />';

Hope this helps.

One Comment on “Get Store Information in Magento 2”

Leave a Reply

Your email address will not be published. Required fields are marked *