2

How I can get fetch the welcome text of Configuration in Template file.

For e.g. welcome text : "DEFAULT WELCOME MSG!"

I know I can get it in Magento 1.9 with:

<?php echo Mage::getStoreConfig('design/header/welcome') ?>

But now sure how to fetch it in Magento 2.x

Prince Patel
  • 22,708
  • 10
  • 97
  • 119
Omer
  • 151
  • 1
  • 8

3 Answers3

3

With objectManager in your phtml file:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$welcome = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('design/header/welcome');
echo $welcome;

With Factory method:

In Your Helper

<?php
namespace Vendor\Module\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    public function getConfig($config_path)
    {
        return $this->scopeConfig->getValue(
            $config_path,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }
}

In phtml

echo $this->helper('Vendor\Module\Helper\Data')->getConfig('design/header/welcome');

Note: Don't use objectManager instance directly in files as EcgM2 coding standard.

Prince Patel
  • 22,708
  • 10
  • 97
  • 119
  • 1
    Note: Don't use objectManager directly in template files. ... Why is that? – Omer Jun 10 '17 at 19:22
  • with object manager it worked ...

    Can you explain why I should not use it and what is helper? I don't have any helper

    – Omer Jun 10 '17 at 19:26
  • 1
    There are several reasons. The code will work, but it is best practice to not reference the ObjectManager class directly. Please refer this for more details https://magento.stackexchange.com/q/117098. But If you haven't custom module you can use objectManager in the template. – Prince Patel Jun 11 '17 at 05:33
0

Take a look: vendor\magento\module-theme\Block\Html\Header.php

public function getWelcome()
{
    if (empty($this->_data['welcome'])) {
        $this->_data['welcome'] = $this->_scopeConfig->getValue(
            'design/header/welcome',
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }
    return __($this->_data['welcome']);
}

As we can see, in your block, we can use $this->_scopeConfig->getValue(...)

Khoa TruongDinh
  • 32,054
  • 11
  • 88
  • 155
0

If the template you are trying to retrieve the store-config from already has a block associated with it that has scopeConfig injected then you can call the function from the block directly in your template, if not, you will need to create a block that has a function that can interact with scopeConfig like below.

You can programmatically retrieve store config by creating a helper function like so in your module .

# app/code/Vendor/Module/Helper/Data.php

<?php

namespace Vendor\Module\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

class Data extends AbstractHelper
{
    public function getConfig($config_path)
    {
        return $this->scopeConfig->getValue(
            $config_path,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }
} 

and inject where you need it if already not injected.

# app/code/Vendor/Module/Model/<filename>/php

<?php

namespace Vendor\Module\Model;

use Vendor\Module\Helper\Data as Helper;

class SomeClass
{
    protected $_helper;

    public function __construct(Helper $helper)
    {
        $this->_helper = $helper;
    }

    public function getEmailAddress()
    {
        return $emailAddress = $this->_helper->getConfig('general/contact/email');
    }
}

To find the format of the configuration data you need to retrieve, go to the core module, for which the data needed is, and look in vendor/magento/module-<name>/etc/adminhtml/system.xml the format will be section/group/field so you are looking for <section id="<section_name>..., <group id="<group_name>..., and <field id="<field_name..., just grab the id’s and put them in order as the getConfig param i.e. $this->helper->getConfig('section/group/field');

I wrote a blog entry here to retrieve store config in your module. http://magefoo.com/programmatically-retrieve-store-config-data/

djfordz
  • 342
  • 1
  • 9