Get store code value from current store programmatically in Magento 2.

You can get the current store’s, store code value in Magento 2.

You need to use Magento\Store\Model\StoreManagerInterface interface for fetching current store locale.

I have given a demo using Block Class, Continue reading “Get store code value from current store programmatically in Magento 2.”

How to get current store information in Magento 2?

You can get Current store related data like store id, store name, group id, website id and available currency code using Magento 2 by just simple code snippets.

<?php
namespace Rbj\StoreInfo\Block;

class StoreInfo extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        array $data = []
    ) {
        $this->_storeManager = $storeManager;
        parent::__construct($context, $data);
    }

    /**
     * @return Store
     */
    public function getStore()
    {
        return $this->_storeManager->getStore();
    }

Call from Template file for getting the data of store,

echo $block->getStore()->getName(); // Default Store view
echo $block->getStore()->getCode(); // default
echo "<pre>";print_r($block->getStore()->getData());

Output for Current Store looks like below array,

Array
(
    [store_id] => 1
    [code] => default
    [website_id] => 1
    [group_id] => 1
    [name] => Default Store View
    [sort_order] => 0
    [is_active] => 1
    [available_currency_codes] => Array
        (
            [0] => USD
        )

)

Want to Know about Next blog, Get All Root Categories Ids in Magento 2.

How to get default website id in Magento 2?

For Multi website setup of Magento e-commerce site, Sometimes need to get Default Website id using programmatically, You need to refer below code snippet for getting default website id.

Hope, You are aware of how to get website collection and other details using coding.
Create Block file,

<?php
namespace Rbj\Website\Block;

class GetWebsite extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        array $data = []
    ) {
        $this->_storeManager = $storeManager;
        parent::__construct($context, $data);
    }

    /**
     * @return string|int
     */
    public function getDefaultWebsiteId()
    {
        return $this->_storeManager->getDefaultStoreView()->getWebsiteId();
    }

Call from template file or PHP file,

echo $this->getDefaultWebsiteId(); // output is your default website code.
For single website output is 1.