59

Magento 1.x use Mage::getStoreConfig('sections/groups/fields') get data on core_config_data table.

How to Magento2 get value from core_config_data table ?

6 Answers 6

140

We need to call the default method available.

Just use \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, in your constructor argument and set the class property: $this->scopeConfig = $scopeConfig;

Now to get the configuration value just use

$this->scopeConfig->getValue('dev/debug/template_hints', 
    \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

I have get the answer from this link and refer this

7
  • 4
    good one Meenakshi! keep it up
    – anshuman
    Jan 11, 2017 at 16:34
  • How do you obtain them from a separate script, not from within the magento filesystem or a module? If you make a class in your script and instantiate it, what should the class inherit from, and how do you ensure it loads all the proper files?
    – ahnbizcad
    Jan 18, 2018 at 22:19
  • Which scope does this get the configuration from? The currently viewed storeview by the customer?
    – Erfan
    Jan 24, 2018 at 8:11
  • yes, it'll take current storeview of the customer Jan 24, 2018 at 8:13
  • how to use the same using object manager? Aug 16, 2018 at 10:24
4

Create a function for getting configuration values in your custom module's helper.

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

and call anywhere you want for example in test.phtml

$moduleStatus = $this->helper('Customvendorname\Custommodulename\Helper\Data')->getConfig('sectionid/groupid/fieldid');

In block and helper call like this:

 $this->_objectManager->create('Customvendorname\Custommodulename\Helper\Data')->getConfig('sectionid/groupid/fieldid');
5
  • 8
    It's recommended in M2 to prefer the DI approch. Thus, using $this->_objectManager->create(...) should be avoided. Jun 22, 2016 at 12:27
  • Thanks @AnnaVölkl, then what is the replacement of that please can you post a similar answer here ?
    – Manish
    Jun 22, 2016 at 13:12
  • See the upvoted answer above. Jun 22, 2016 at 13:22
  • please tell me how to find out path/of/config. I am trying to find the configuration value for the enabled section of Payment method Jun 23, 2020 at 16:25
  • @CodeForGood this is defined in your module >>etc >>system.xml. For any system configuration setting, field is defined in the following manner 'section' >> group >> field' so your path will be ''sectionid/groupid/fieldid'
    – Manish
    Jun 25, 2020 at 10:57
3

I have used the following method to retrieve the variables,

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

return $this->_data['welcome'];
2
  • please tell me how to find out path/of/config. I am trying to find the configuration value for the enabled section of Payment method Jun 23, 2020 at 16:25
  • 1
    You can get the information from system.xml file
    – mjdevloper
    Jun 24, 2020 at 13:41
0

Most likely your class already has a Magento\Framework\App\Helper\Context injected. You can get a ScopeConfig from there:

$this->context->getScopeConfig()->getValue(
    'sections/groups/fields',
    \Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
1
  • please tell me how to find out path/of/config. I am trying to find the configuration value for the enabled section of Payment method Jun 23, 2020 at 16:25
0

First you need to include the Magento\Store\Model\ScopeInterface class in your constructor:

protected $_scopeConfig;

public function __construct(
     ...
     \Magento\Store\Model\ScopeInterface $scopeInterface,
     ...
)
{
    ...
    $this->_scopeConfig = $scopeInterface;
    ...
}

Then in your class' method you can call the following:

$this->scopeConfig->getValue('path/of/config', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
3
  • please tell me how to find out path/of/config. I am trying to find the configuration value for the enabled section of Payment method. Jun 23, 2020 at 16:24
  • you got this from core_config_data table Jun 24, 2020 at 7:01
  • @CodeForGood this is defined in your module >>etc >>system.xml. For any system configuration setting, field is defined in the following manner 'section' >> group >> field' so your path will be ''sectionid/groupid/fieldid
    – Manish
    Jun 25, 2020 at 11:53
0

In template (phtml) files, I use in the following way:

$config = $block->getLayout()->createBlock(\Magento\Config\Block\System\Config\Form::class);
$configValue = $config->getConfigValue('web/secure/base_url');

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.