且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

覆盖 Magento 配置

更新时间:2023-11-29 23:25:58

Magento 在运行时直接从配置对象的树结构中读取其配置值,因此需要使用配置对象的原生 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.

使用当前版本的 Magento(我相信,但尚未使用旧版本测试),您需要在当前 store 的节点集中设置配置值.

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');

请记住,这一切都需要在 Magento 引导配置对象之后发生.还要记住,有一段时间 Magento 会加载配置,但不会加载 store 对象.如果是这种情况,您将无法从商店对象加载商店代码.

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 模块中做了类似的事情.如果您对工作代码感兴趣,请访问 Github.

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