How To Get Data From System Configuration In Magento 2

Are you a beginner in Magento 2 development? If so, I guess you are on your first tutorial on Magento 2 module development!

And hence, the below solution to get data from system configuration in Magento 2 is going to be used often. You might as well bookmark this post!

For example, you want to get the data of Magento 2 extension status, if it is enabled or disabled.

Magento 2 allows creating custom configuration fields and get config data programmatically from other modules or core Magento!

Method To Get Data From System Configuration In Magento 2:

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

protected $scopeConfig;

public function __construct(
    ScopeConfigInterface $scopeConfig
) {
    $this->scopeConfig = $scopeConfig;
}


public function getConfigValue() {
    return $this->scopeConfig->getValue("sectionId/groupId/fieldId",ScopeInterface::SCOPE_STORE);
}

That’s it. To make your extensions work expectedly certain configurations are made to have particular values for that you need to set default values for Magento 2 system configuration.

Do share the post with the Magento community via social media.

Thanks.

Table of Contents