問(wèn)題描述
我正在尋找一個(gè)很好的解決方案來(lái)覆蓋 Magento 配置而不更改默認(rèn)值.
I am looking for a good solution to override the Magento config without changing the default values.
例如,我想在不刪除現(xiàn)有值的情況下覆蓋 core_config_data 表中的web/unsecure/base_skin_url"項(xiàng).因此,如果代碼中的任何地方都調(diào)用了此確切代碼:
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');
它會(huì)找到我設(shè)置的配置選項(xiàng),而不是默認(rèn)的...
It will find the config option I set and not the default one...
提前致謝!
查克
推薦答案
Magento 在運(yùn)行時(shí)直接從配置對(duì)象的樹(shù)結(jié)構(gòu)中讀取其配置值,因此需要使用配置對(duì)象的原生 setNode
方法更改值.但是,由于 Magento 加載范圍配置(自鏈接)的方式,它并不那么直接向前看.
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.
使用當(dāng)前版本的 Magento(我相信,但尚未使用舊版本測(cè)試),您需要在當(dāng)前 store
的節(jié)點(diǎn)集中設(shè)置配置值.
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
.
第一步是獲取當(dāng)前設(shè)置的商店的代碼.您可以通過(guò)以下方式以編程方式執(zhí)行此操作
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();
然后,您可以通過(guò)以下調(diào)用設(shè)置配置值
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');
請(qǐng)記住,這一切都需要在 Magento 引導(dǎo)配置對(duì)象之后發(fā)生.還要記住,有一段時(shí)間 Magento 會(huì)加載配置,但不會(huì)加載 store 對(duì)象.如果是這種情況,您將無(wú)法從商店對(duì)象加載商店代碼.
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.
我在我的 Pulse Storm Chaos 模塊中做了類(lèi)似的事情.如果您對(duì)工作代碼感興趣,請(qǐng)?jiān)L問(wèn) Github.
I did something similar in my Pulse Storm Chaos module. If you're interested in working code it's on Github.
這篇關(guān)于覆蓋 Magento 配置的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!