44

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 ?

115

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

| improve this answer | |
  • 4
    good one Meenakshi! keep it up – anshuman Jan 11 '17 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 '18 at 22:19
  • Which scope does this get the configuration from? The currently viewed storeview by the customer? – Erfan Jan 24 '18 at 8:11
  • yes, it'll take current storeview of the customer – MeenakshiSundaram R Jan 24 '18 at 8:13
  • @MeenakshiSundaramR I am asking apart from this post, how to give free shipping for selected products using sku.Help me at magento.stackexchange.com/q/215022/57334 – zus Feb 28 '18 at 7:00
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');
| improve this answer | |
  • 7
    It's recommended in M2 to prefer the DI approch. Thus, using $this->_objectManager->create(...) should be avoided. – Anna Völkl Jun 22 '16 at 12:27
  • Thanks @AnnaVölkl, then what is the replacement of that please can you post a similar answer here ? – Manish Jun 22 '16 at 13:12
  • See the upvoted answer above. – Anna Völkl Jun 22 '16 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 – CodeForGood Jun 23 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 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'];
| improve this answer | |
  • 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 – CodeForGood Jun 23 at 16:25
  • 1
    You can get the information from system.xml file – mjdevloper Jun 24 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
);
| improve this answer | |
  • 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 – CodeForGood Jun 23 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);
| improve this answer | |
  • 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. – CodeForGood Jun 23 at 16:24
  • you got this from core_config_data table – Baharuni Asif Jun 24 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 at 11:53

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.