This article is produced with scandiweb's eCommerce expertise

Collaborate with our development, PPC, SEO, data & analytics, or customer experience teams to grow your eCommerce business.

Magento Series: Working with Store Configs

Magento (Adobe Commerce) modules often need to keep track of the store configuration when implementing backend logic. This article talks about:

  • How configuration values are stored
  • How to get configuration values
  • How to set configuration values programmatically
  • How to specify default values

How are configuration values stored?

Magento (Adobe Commerce) maps a set of configuration paths (e.g., web/secure/base_url) to their values (e.g., https://localhost/). This configuration is scope-based, meaning you can define a “base” configuration value for the default scope, then override it for a specific store.

The configuration is stored in the core_config_data table and easily accessible using a database exploration tool.

In addition to the configuration key and value, the table stores the scope (default/store by id/website by id) and the timestamp when the configuration was last updated.

How to get configuration values?

The \Magento\Framework\App\Config\ScopeConfigInterface provides functions for retrieving a configuration value.

The getValue function takes 1 to 3 arguments:

  1. The configuration value path: e.g., general/store_information/name
  2. The scope: store/ default/ website (optional, default if not specified)
  3. The store/website ID (not required for default scope)

➡️ There’s also the isSetFlag function for boolean configurations

For example, to get the store name of a store with a known ID:

$storeName = $this->scopeConfig->getValue(
    Information::XML_PATH_STORE_INFO_NAME,
    ScopeInterface::SCOPE_STORE,
    $storeId
);

➡️ The path is often taken from a PHP constant, like Information::XML_PATH_STORE_INFO_NAME. This helps to avoid repetition and typos. It also makes refactoring easier—if the path changes, only one constant needs to change!

How can I set configuration values programmatically?

Often, configuration values are edited in the Magento admin panel. However, you may sometimes wish to edit them programmatically, such as in a migration script or a custom controller.

The \Magento\Framework\App\Config\Storage\WriterInterface provides functions for setting or clearing a configuration value.

The delete function is for clearing a config value, and takes 1 to 3 arguments, same as the getValue function of ScopeConfigInterface:

  1. The configuration value path: e.g., general/store_information/name
  2. The scope: store/ default/ website (optional, default if not specified)
  3. The store/website ID (not required for default scope)

The save function sets a config value and takes 2 to 4 arguments. In addition to the 3 arguments of delete, it requires the value to set the config too. The arguments are:

  1. The configuration value path: e.g., general/store_information/name
  2. The configuration value to set: e.g., Amazing Store
  3. The scope: store/ default/ website (optional, default if not specified)
  4. The store/website ID (not required for default scope)

Example:

$this->configWriter->save(self::CRON_STRING_PATH, join(' ', self::CRON_EXPR_ARRAY));

How to specify default values?

The default value will be used if a configuration value is not specified. You can specify the default values in the etc/config.xml file.

For example, to specify the default value "{{base_url}}" for web/unsecure/base_url and "{{unsecure_base_url}}” for web/unsecure/base_web_url, you would write:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <web>
            <unsecure>
                <base_url>{{base_url}}</base_url>
                <base_web_url>{{unsecure_base_url}}</base_web_url>
            </unsecure>
        </web>
    </default>
</config>

Need help with Magento (Adobe Commerce) development? scandiweb is the most certified Magento team in the world—get your dedicated eCommerce team today or get in touch with one of our experts.

Hire eCommerce experts

Get in touch for a free consultation.

Your request will be processed by

If you enjoyed this post, you may also like