How to Get Value from Store Configuration by Scope in Magento 2?

Steps to Get Store Config Value in Magento 2

In order to fetch system store configuration values by scope level on store or website, you will be required to use ScopeConfigInterface with getValue() method.

It is easy to catch the core config data table value programmatically using the below mentioned method:

public function __construct(
 \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
 \Magento\Store\Model\StoreManagerInterface $storeManager
) {
 $this->scopeConfig = $scopeConfig;
 $this->storeManager = $storeManager;
}
Now call a function,

public function getConfigValue() {
 return $this->scopeConfig->getValue("sectionId/groupId/fieldId",
 \Magento\Store\Model\ScopeInterface::SCOPE_STORE,$this->storeManager->getStore()->getStoreId();
}

The first argument sectionId/groupId/fieldId is from your etc/adminhtml/system.xml file.

The second argument will be the scope value.

The third argument will be scopeCode. It may be int, string, or null value.

Demo system.xml to illustrate the example,

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
 <system>
 <section id="test" translate="label" type="text" sortOrder="310" showInDefault="1" showInWebsite="1" showInStore="1">
 <label>Test Settings</label>
 <group id="origin" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0">
 <label>Main label</label>
 <field id="postcode" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
 <label>Postal Code</label>
 </field>
 </group>
 </section>
 </system>
</config>

In our case test/origin/postcode is the key value for sectionId/groupId/fieldId.

We hope we have got everything covered to help you get value from store configuration by scope in Magento 2. If we missed out anything, feel free to get in touch with us.