Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override Magento Config

Tags:

php

magento

I am looking for a good solution to override the Magento config without changing the default values.

For example, I want to override the "web/unsecure/base_skin_url" item in the core_config_data table without deleting the existing value. So if anywhere in the code this exact code is called:

Mage::getStoreConfig('web/unsecure/base_skin_url');

It will find the config option I set and not the default one...

Thanks in advance!

Chuck

like image 621
Chuck D Avatar asked Oct 10 '12 22:10

Chuck D


3 Answers

Magento reads its configuration values at runtime directly from the configuration object's tree structure, so you need to use the configuration object's native setNode method to change the values. However, because of the way Magento loads in scoped configuration (self link), it's not as straight forward as it seems.

With current versions of Magento (and I believe, but have not tested, with older versions), you'll need to set the configuration value in the set of nodes for the current store.

Step one is getting the code for the currently set store. You can do this programmatically with the following

$store = Mage::app()->getStore();
$code  = $store->getCode();

then, you can set a configuration value with the following call

$config = Mage::getConfig();
$config->setNode("stores/$code/web/unsecure/base_skin_url", 'value_to_set');

Keep in mind this all needs to happen after Magento has bootstrapped the configuration object. Also keep in mind there's a period of time where Magento will have a loaded configuration, but the store object will not be loaded. If this is the case you will not be able to load the store code from the store object.

I did something similar in my Pulse Storm Chaos module. If you're interested in working code it's on Github.

like image 181
Alan Storm Avatar answered Nov 06 '22 12:11

Alan Storm


Alan's answer is correct, but it doesn't care about the config cache. For example, if you call Mage::getStoreConfig('web/unsecure/base_skin_url') twice and change the value in between, the change has no effect. To get around this issue, you should use $store->setConfig('web/unsecure/base_skin_url', 'value_to_set'). It does both: update the config cache and set the config node with Alan's method.

like image 43
Andreas von Studnitz Avatar answered Nov 06 '22 12:11

Andreas von Studnitz


If you want to overwrite some special config data, you can put it in app/etc/local.xml. But thats only useful for your own shop, not for public modules.

Heres a way to overwrite the base_url for development-purposes without altering the database.

<config>
...
    <stores>
        <default>
            <web>
                <unsecure>
                    <base_url>http://dev.myshop.com/</base_url>
                </unsecure>
                <secure>
                    <base_url>http://dev.myshop.com/</base_url>
                </secure>
            </web>
        </default>
        <admin>
            <web>
                <unsecure>
                    <base_url>http://dev.myshop.com/</base_url>
                </unsecure>
                <secure>
                    <base_url>http://dev.myshop.com/</base_url>
                </secure>
            </web>
        </admin>
    </stores>
...
</config>
like image 1
Ralf Siepker Avatar answered Nov 06 '22 10:11

Ralf Siepker