How to Set & Get Data from core_config_data Table in Magento 2?

Today, we’re going to teach you guys how to set and get data from core_config_data table in Magento 2.

By default, Magento 2 does provide us with a variety of configuration options in the backend. But making changes to them largely depends on particular circumstances.

For instance, if you want to make changes to the product visibility based on the store type, you need to do it programmatically as there are no such default configuration options available in the backend.

And in this tutorial, we will show you exactly how to set & Get different data from core_config_data table to make certain changes.

How to Set Configuration Data in Magento 2?

Magento, by default, stores all admin settings in the core_config_data table in the database.

And in that file, you can easily get values through a string, which basically indicates the path to a variable name.

Now, if you want to set data in the core_config_data table, then you need to use the save() function inside \Magento\Framework\App\Config\Storage\WriteInterface/ folder and copy the following code:

interface WriterInterface
{

   /**

    * Delete config value from storage

    *

    * @param   string $path

    * @param   string $scope

    * @param   int $scopeId

    * @return void

    */

   public function delete($path, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0);

   /**

    * Save config value to storage

    *

    * @param string $path

    * @param string $value
interface ScopeConfigInterface
{

   /**

    * Default scope type

    */

   const SCOPE_TYPE_DEFAULT = 'default';

   /**

    * Retrieve config value by path and scope.

    *

    * @param string $path The path through the tree of configuration values, e.g., 'general/store_information/name'

    * @param string $scopeType The scope to use to determine config value, e.g., 'store' or 'default'

    * @param null|string $scopeCode

    * @return mixed

    */

   public function getValue($path, $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null);

   /**

    * Retrieve config flag by path and scope

    *

    * @param string $path The path through the tree of configuration values, e.g., 'general/store_information/name'

    * @param string $scopeType The scope to use to determine config value, e.g., 'store' or 'default'

    * @param null|string $scopeCode

    * @return bool

    */

   public function isSetFlag($path, $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null);

}

In the above code:

$Path is config path that you need to set for your new value.

$Value is your new value.

$Scope can be anything from store, group, website which is found in the app/code/Magento/Store/Model/ScopeInterface.php or in the /Magento/Framework/App/ScopeInterface.php.

$ScopeID is set to 0 automatically by default or you can add it manually by finding the appropriate scopeID in store_group, store, or store_website table.

And that’s about it!

This is how you can set configuration data in Magento 2.

Now, we will look at how to get configuration data in Magento 2.

How to Get Configuration Data in Magento 2

Earlier, we learned how to set configuration data programmatically. But what about retrieving configuration data?

Here are the steps you need to follow to get configuration data programmatically in Magento 2.

First of all, we need to use getValue() function inside Magento\Framework\App\Config\ScopeConfigInterface folder and add the following code:

{

   /**

    * Default scope type

    */

   const SCOPE_TYPE_DEFAULT = 'default';

   /**

    * Retrieve config value by path and scope.

    *

    * @param string $path The path through the tree of configuration values, e.g., 'general/store_information/name'

    * @param string $scopeType The scope to use to determine config value, e.g., 'store' or 'default'

    * @param null|string $scopeCode

    * @return mixed

    */

   public function getValue($path, $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null);

   /**

    * Retrieve config flag by path and scope

    *

    * @param string $path The path through the tree of configuration values, e.g., 'general/store_information/name'

    * @param string $scopeType The scope to use to determine config value, e.g., 'store' or 'default'

    * @param null|string $scopeCode

    * @return bool

    */

   public function isSetFlag($path, $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null);

}

Here, you can find specific functions in the Magento\Framework\App\Config as shown below:

public function getValue(

   $path = null,

   $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,

   $scopeCode = null

) {

   if ($scope === 'store') {

       $scope = 'stores';

   } elseif ($scope === 'website') {

       $scope = 'websites';

   }

   $configPath = $scope;

   if ($scope !== 'default') {

       if (is_numeric($scopeCode) || $scopeCode === null) {

           $scopeCode = $this->scopeCodeResolver->resolve($scope, $scopeCode);

       } elseif ($scopeCode instanceof \Magento\Framework\App\ScopeInterface) {

           $scopeCode = $scopeCode->getCode();

       }

       if ($scopeCode) {

           $configPath .= '/' . $scopeCode;

       }

   }

   if ($path) {

       $configPath .= '/' . $path;

   }

   return $this->get('system', $configPath);

}

Now, we need to call ScopeConfigInterface in the construct function anywhere as shown below:

public function __construct(

   \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig

)

{

   $this->_scopeConfig = $scopeConfig;

}

Then, we now need to use getValue() function as following:

$this->_scopeConfig->getValue($path, $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null);

$Path is config path that you would like to set for new value.

$ScopeType is used for determining config value. This can be store, group, or website, which is usually found in app/code/Magento/Store/Model/ScopeInterface.php or in /Magento/Framework/App/ScopeInterface.php

$ScopeCode is set to 0 automatically by default or you can find an appropriate ScopeCode like ‘Store’ scope in the matching table.

And that’s it!

Also read: How to Create a Custom Database Table in Magento 2?

Conclusion

This is how you can set & get configuration data programmatically from core_config_data table in Magento 2.

And if you need our professional assistance, feel free to contact us anytime.

Magento Support Services