8

I am trying to add the store's phone number setup in Stores > Configuration > General > General > Store Information to the header in my theme. In a module I think this could be done using getValue() in \Magento\Framework\App\Config\ScopeConfigInterface but I cannot see a way how to use this inside a theme. So far I have added this to default.xml

 <referenceContainer name="header-wrapper">
      <block class="Magento\Framework\View\Element\Template" name="store.phone.number" template="Magento_Theme::phone.phtml" />
 </referenceContainer>

but I do not know how to get the phone number inside phone.phtml

13

I suggest you create your own block, which will extend the Magento\Framework\View\Element\Template class.

As the Magento\Framework\App\Config\ScopeConfigInterface is part of Magento\Framework\View\Element\AbstractBlock (the parent of the Template class) declared in $_scopeConfig, you can add the following function to your custom block:

public function getConfig()
{
    return $this->_scopeConfig;
}

Then in your template you can do:

$block->getConfig()->getValue('value/you/need');

Don't forget to update your layout like this:

<referenceContainer name="header-wrapper">
      <block class="Vendor\Module\Block\View\Element\Template" name="store.phone.number" template="Magento_Theme::phone.phtml" />
</referenceContainer>
  • Having to create a module to work with the theme doesn't seem a very elegant way of doing it (although it looks like it might be the only way). Surely this will cause problems for theme developers that are publishing themes – Alex Apr 22 '16 at 9:18
  • @Alex yes using the object manager directly is definitely easier but it is not recommended to use it directly. So there's two options, quick and dirty or slow and clean ^^ – Raphael at Digital Pianism Apr 22 '16 at 9:20
  • @Alex Theme developers have always shipped their themes with theme specific modules and I don't know of any problems. It actually is an elegant solution. Having much PHP code in templates isn't. – Fabian Schmengler Apr 22 '16 at 14:36
6

We can directly get a store config in the template by getting instance of Magento\Framework\App\Config\ScopeConfig:

 \Magento\Framework\App\ObjectManager::getInstance()
  ->get('Magento\Framework\App\Config\ScopeConfigInterface')
  ->getValue('value/you/need');

Technically, when someone asks for an instance of Magento\Framework\App\Config\ScopeConfigInterface, we will give it an instance of the Magento\Framework\App\Config\ScopeConfig. For example, we can get the default setting for grid or list mode:

$productListMode = \Magento\Framework\App\ObjectManager::getInstance()
   ->get('Magento\Framework\App\Config\ScopeConfigInterface')
   ->getValue('catalog/frontend/list_mode');

Note: Avoiding Using Object Manager directly. We should keep our templates clean. Try to add config to the block. Should follow the @Raphael answer.

  • Thanks for the answer, it works for catalog/frontend/list_mode like in your example but is returning null for general/store_information/phone – Alex Apr 22 '16 at 9:15
  • Did you set the phone number for your store? I can get the phone number of store: \Magento\Framework\App\ObjectManager::getInstance() ->get('Magento\Framework\App\Config\ScopeConfigInterface') ->getValue('general/store_information/phone'); – Khoa TruongDinh Apr 22 '16 at 10:02
  • Yeh I added in Stores > Configuration > General > General > Store Information. Tried a default value and for the website/store – Alex Apr 22 '16 at 10:09
  • Please try to re-index and clear cache. – Khoa TruongDinh Apr 22 '16 at 10:13
  • I've got it pulling the default phone number now but is ignoring the one set in the store/website config. Have you tried this? – Alex Apr 22 '16 at 10:29
2

Try this on Block, it is working for me after many search

$isEnabled = \Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\Framework\App\Config\ScopeConfigInterface') ->getValue('section/group/field');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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