How to Get Store Config Value in Magento 2?

This guide will help you with steps about how to get the store config value in Magento 2. This platform allows to set configuration value by different scopes like specific website, store and store view. So, it’s required to get store configuration value by scope in Magento 2. Generally, when a user wants to access a certain value in the whole website, store configuration needs to be created and setting value is required to access value everywhere.

Steps to Get Store Config Value in Magento 2

In Magento 2, there is a configuration value set in the core_config_data table. So, Let’s follow the below steps.

Let’s assume that you have created a module. Now, you need to create a helper file in your module at app/code/MD/Helloworld/Helper/Data.php

<?php

namespace MD\Helloworld\Helper;

use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\Helper\AbstractHelper;

class Data extends AbstractHelper
{
    const XML_CONFIG_PATH = 'config/to/path';

    public function getConfig()
    {
        $configValue = $this->scopeConfig->getValue(self::XML_CONFIG_PATH,ScopeInterface::SCOPE_STORE); // For Store
        /**
         * For Website
         * 
         * $configValue = $this->scopeConfig->getValue(self::XML_CONFIG_PATH,ScopeInterface::SCOPE_WEBSITE);
         */
         return $configValue;
    }
}

Now, you can inject the helper class anywhere and use the getConfig() function to get config value.

Hope this guide helped you to get store config value in the Magento 2 store. Let us know if we missed out anything and if you get stuck anywhere. Our Magento experts will be glad to help you and solve all your problems.

Also read: How to Get Config Value in Layout XML in Magento 2?