How to Get Store Config Value in Magento 2

Magento 2 allows you to set configuration value by different scopes like specific website, specific store and specific store view. So, It’s required to get store configuration value by scope in Magento 2. Generally, When user wants to access the value in whole website at that time, store configuration needs to create and set value in that to access value everywhere.

In Magento 2, there is all 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/Kaushik/Helloworld/Helper/Data.php

<?php
namespace Kaushik\Helloworld\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

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

    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $scopeConfig;

    /**
     * @param  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     */
    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    ) {
        $this->scopeConfig = $scopeConfig;
    }

    public function getConfig($path, $storeId = null)
    {
        return $this->scopeConfig->getValue(
            $path,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
            $storeId
        );
    }
}

 

Leave a Reply