15

How is it possible to output the current store name in a Magento 2 custom template with a custom block?

2
  • what template??
    – Marius
    Commented Dec 14, 2015 at 8:44
  • custom template using a custom block Commented Dec 14, 2015 at 9:06

3 Answers 3

22

you need to use the instance of \Magento\Framework\App\Config\ScopeConfigInterface in your block:

Create the method getStoreName()

public function getStoreName()
{
    return $this->_scopeConfig->getValue(
        'general/store_information/name',
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE
    );
}

and call in your template echo $this->getStoreName()

3
  • Loos like a great solution, but I get the following error: Fatal error: Call to a member function dispatch() on a non-object in /vendor/magento/framework/View/Element/AbstractBlock.php on line 637. Commented Dec 14, 2015 at 9:36
  • clear the var/generation folder
    – Marius
    Commented Dec 14, 2015 at 9:37
  • 1
    @Marius 's approach is right, but it didn't work for me. Instead I've used \Magento\Store\Model\StoreManagerInterface $storeManager in constructor and public function getStoreName() { return $this->storeManager->getStore()->getName(); } Instead of getName() you can use getCode(), getId().
    – Razvan
    Commented May 19, 2016 at 9:37
11

Use store manager, which holds information about active store. If custom block is not inherited from Template block, inject dependency on \Magento\Store\Model\StoreManagerInterface in construct.

<?php
namespace VendorName\ModuleName\Block;

class CustomBlock extends \Magento\Framework\View\Element\Template
{
    /**
     * Get current store name.
     *
     * @return string
     */
    public function getCurrentStoreName()
    {
        return $this->_storeManager->getStore()->getName();
    }
}

Then in template:

<?php
/**
 * @var $block \VendorName\ModuleName\Block\CustomBlock
 */
echo "<h1>Current store name is '{$block->getCurrentStoreName()}'</h1>";
?>
2
  • Thanks for your solution, but I don't want to show the store view name.Searched for the store name of the configuration. Commented Dec 14, 2015 at 10:38
  • It always returns 'Default Store View' instead of my store name
    –  coinhndp
    Commented Jul 28, 2020 at 10:52
5

To get a store configuration value like general/store_information/name you can use the following

$config = new \Magento\Framework\App\Config\ScopeConfigInterface();

echo $config->getValue('general/store_information/name');

However, doing this from a block or helper would be cleaner. Below is a helper class that would exist in your own custom module

namespace [Namespace]\[Module]\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * Retrieve store name
     *
     * @return string|null
     */
    public function getStoreName()
    {
        return $this->scopeConfig->getValue(
            'general/store_information/name',
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }
}

Which you would inject as dependency in your block class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.