How To Call Store ID in Magento 2.x

There are following below code , to call Store ID in Magento 2 by using Depdependency Injection.

<?php
namespace Mage2db\John\Block;
class Index 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();
    }
}
?>

Call above block method inside template phtml file.

echo"<BR> Store ID=".$block->getStoreId();
echo"<BR> Store Code=".$block->getStoreCode();
echo"<BR> Store Website ID=".$block->getWebsiteId();
echo"<BR> Store Name=".$block->getStoreName();
echo"<BR> Store URL=".$block->getStoreUrl();
echo"<BR> Store Active Yes / No=". $block->isStoreActive();

Leave a Reply

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