Question

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 ?

Was it helpful?

Solution

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

OTHER TIPS

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');

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'];

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
);

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);

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');
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top