Overriding Magento Configuration without Changing Default Values
Hello everyone! Today we’re going to tackle an interesting question from Chuck about overriding Magento configuration values without deleting or modifying the default settings.
Understanding Magento’s Core Config Data
Magento stores its configuration data in the core_config_data table by default. Each row represents a single configuration option, with columns for scope, scope_id, path, and value.
For example, the default value for “web/unsecure/base_skin_url” might look something like this:
id | scope | scope_id | path | value
----+-----------+----------+-----------------------------+------
1 | 0 | 0 | web/unsecure/base_skin_url | skin/
The Challenge: Overriding a Single Option
Chuck wants to override this specific value without changing the default one. This means we need to add a new row to the table while preserving the existing configuration.
To achieve this, let’s create a custom module that will insert our override during installation.
Step-by-Step Solution
- Create your custom module
namespace LaravelCompany\MyModule;
class InstallData implements \Magento\Framework\Setup\InstallDataInterface
- Override the value in the install script
public function install(\Magento\Framework\Setup\SchemaSetupInterface $setup, \Magento\Framework\Setup\ModuleContextInterface $context)
{
$setup->startSetup();
$setup->getConnection()->insert(
$setup->getTable('core_config_data'),
[
'scope' => 'default',
'scope_id' => 0,
'path' => 'web/unsecure/base_skin_url',
'value' => '/my/new/skin/path/',
]
);
$setup->endSetup();
}
In this script, we’re using the insert method to add a new row with our desired override value. Notice that we’re preserving the default scope and scope_id values.
- Enable your module
After creating this installation data file in your module’s etc/install-data.php directory, enable your module via Magento’s admin interface or by running Magento’s setup:upgrade command.
Retrieving Your Override
Now, whenever you call Mage::getStoreConfig('web/unsecure/base_skin_url'), it will return our custom override value instead of the default one.
$baseSkinUrl = Mage::getStoreConfig('web/unsecure/base_skin_url');
echo $baseSkinUrl; // Outputs: /my/new/skin/path/
Conclusion
Overriding Magento configuration values without modifying defaults is a powerful technique for maintaining flexibility in your store’s settings.
By using this approach, you can customize specific configurations without affecting the core system. It allows you to have your customizations exist alongside the default values, providing cleaner code and easier management.
Remember to always follow best practices when creating modules, such as keeping them organized and documenting their purpose clearly.
That’s all for today! I hope this solution helps you manage your Magento configuration more effectively. If you have any further questions or need help with other aspects of Magento development, feel free to reach out!
Happy coding!