When doing customization or building custom solutions in Magento 2, you may have a case where you need to retrieve store information programmatically.

In this article, we will show you how to retrieve the Current Store ID, Name, Code, Status, URL, Website ID programmatically, via Model, Controller, Block, and phtml template.

1. Using Model

You can create your custom model Vendor\Module1\Model\MyConfig where you fetch all store information, and use it further when needed.

Example:

<?php

namespace Vendor\Module1\Model;

use Magento\Store\Model\StoreManagerInterface;

class MyConfig
{
    /**
     * @var StoreManagerInterface
     */
    private $storeManager;

    /**
     * @param StoreManagerInterface $storeManager
     */
    public function __construct(
        StoreManagerInterface $storeManager
    ) {
        $this->storeManager = $storeManager;
    }

    /**
     * Get current store id
     * 
     * @return int
     */
    public function getCurrentStoreId()
    {        
        return $this->storeManager->getStore()->getId();
    }

    /** 
     * Get current store code
     * 
     * @return string
     */
    public function getCurrentStoreCode()
    {        
        return $this->storeManager->getStore()->getCode);
    }	

    /**
     * Get current store name
     * 
     * @return string
     */
    public function getCurrentStoreNmae()
    {        
        return $this->storeManager->getStore()->getName);
    }

    /**
     * Check current store status
     * 
     * @return bool
     */
    public function isCurrentStoreActive()
    {        
        return $this->storeManager->getStore()->isActive();
    }

    /**
     * Get current store URL
     * 
     * @return string
     */
    public function getCurrentStoreUrl()
    {        
        return $this->storeManager->getStore()->getCurrentUrl();
    }
	
    /**
     * Get current website ID
     * 
     * @return int
     */
    public function getCurrentWebsiteId()
    {        
        return $this->storeManager->getStore()->getWebsiteId();
    }	
}

replace: Vendor\Module with your vendor and module name

2. Using Controller

You can inject your custom model Vendor\Module1\Model\MyConfig in your controller construction to retrieve all store information.

3. Using Block

Magento 2, out of the box includes storeManager in \Magento\Framework\View\Element\Template class. You can extend that class in your block to get store information.

Example:

<?php

namespace Vendor\Module1\Block;

class MyBlock extends \Magento\Framework\View\Element\Template
{
    /**
     * Get current store id
     * 
     * @return int
     */
    public function getCurrentStoreId()
    {        
        return $this->_storeManager->getStore()->getId();
    }

    /** 
     * Get current store code
     * 
     * @return string
     */
    public function getCurrentStoreCode()
    {        
        return $this->_storeManager->getStore()->getCode);
    }	

    /**
     * Get current store name
     * 
     * @return string
     */
    public function getCurrentStoreNmae()
    {        
        return $this->_storeManager->getStore()->getName);
    }

    /**
     * Check current store status
     * 
     * @return bool
     */
    public function isCurrentStoreActive()
    {        
        return $this->_storeManager->getStore()->isActive();
    }

    /**
     * Get current store URL
     * 
     * @return string
     */
    public function getCurrentStoreUrl()
    {        
        return $this->_storeManager->getStore()->getCurrentUrl();
    }
	
    /**
     * Get current website ID
     * 
     * @return int
     */
    public function getCurrentWebsiteId()
    {        
        return $this->_storeManager->getStore()->getWebsiteId();
    }	
}

replace: Vendor\Module with your vendor and module name

4. Using PHTML

In the phtml template file, you can get store information from your custom block Vendor\Module1\Block\MyBlock

Example:

<?= $block->escapeHtml($block->getCurrentStoreId()) ?>
<?= $block->escapeHtml($block->getCurrentStoreCode()) ?>
<?= $block->escapeHtml($block->getCurrentStoreName()) ?>
<?= $block->escapeHtml($block->isCurrentStoreActive()) ?>
<?= $block->escapeHtml($block->getCurrentStoreUrl()) ?>
<?= $block->escapeHtml($block->getCurrentWebsiteId()) ?>

Conclusion

We hope you find this guide to "get store information programmatically" helpful. Feel free to share or leave a comment below. Your opinion is much appreciated!

If you're looking for experienced Magento 2 developers to customize your site, fix issues, develop custom solutions, or anything else, feel free to check on this collection of Quality Services For Magento 2, choose the service that best suits your needs, or contact us

Tags: magento-2-backend-development