php - Magento 事件观察器或自定义模块在生产中不起作用

标签 php magento observers

我正在尝试为 Magento 使用多个观察者类和模块,所有这些都被放入 app/code/local 中。这些都在我的本地工作站上工作,但当我通过 FTP 将它们复制到生产环境时将无法工作。我快要发疯了,想弄清楚发生了什么。我所有的代码看起来都很合理。这是一个观察者的例子:

app/code/local/Mural/Pricing/Model/Observer.php

<?php 
class Mural_Pricing_Model_Observer {
    public function setMuralPricing($observer) {
        echo 'We did it!';
        die();
    } 
}

app/code/local/Mural/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Mural_Pricing>
            <version>0.0.1</version>
        </Mural_Pricing>
    </modules>
    <global>
        <models>
            <Mural_Pricing>
                <class>Mural_Pricing_Model</class>
            </Mural_Pricing>
        </models>
    </global>
    <adminhtml>
        <events>
            <sales_quote_add_item>
                <observers>
                    <Mural_Pricing>
                        <class>mural_pricing/observer</class>
                        <method>setMuralPricing</method>
                    </Mural_Pricing>
                </observers>
            </sales_quote_add_item>
        </events>
    </adminhtml>
</config>

应用程序/etc/modules/Mural_Pricing.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Mural_Pricing>
            <codePool>local</codePool>
            <active>true</active>
            <depends>
                <Mage_Catalog />
            </depends>
        </Mural_Pricing>
    </modules>
</config>

就像我说的那样,这在本地运行良好,但在生产环境中却不行,因为有几个不同的观察器和模块。令人沮丧。提前致谢!

最佳答案

我注意到的第一件事是您的观察者声明是调用 mural_pricing/observer::setMuralPricing()关于您观察到的事件。

如果您查看配置 XML,您将在此模块中将模型注册为 Mural_Pricing/...根据这个 XML:

<models>
    <Mural_Pricing>
        <class>Mural_Pricing_Model</class>
    </Mural_Pricing>
</models>

Magento 的最佳实践似乎是对类别名使用全部小写字母,因此我建议您将其更改为 mural_pricing .如果您将观察者声明更改为使用大写等效项,它也会起作用。

另一方面,我认为你给这个标签起什么名字并不重要,但是 <Mural_Pricing>下方标签 <observers>理想情况下应该简短描述您的观察者所做的事情(同样,不要认为它实际上很重要)-例如<mural_pricing_set_pricing> .

最后,必须要说的是 - 确保您已正确清除缓存以重新解析 XML 配置。如果您在生产环境中使用 Redis 或其他缓存架构而不是在开发/暂存/本地环境中使用,那么您可能忘记了以不同于本地方式的方式清除缓存。

最终,除了类别名和观察者声明中的大小写差异之外,您的模块看起来不错。

关于php - Magento 事件观察器或自定义模块在生产中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34192240/

相关文章:

php - 有没有更有效的方法在 mysql 中实现同样的事情(通过 php)

php - 使用 PHP + .htaccess 访问 GET 变量

magento - 如何合并产品描述和附加信息选项卡?

来自观察者的 Magento 网站弹出窗口

php - 我想在我的网站上添加一些检测;这些是好的选择吗?

php - 覆盖 Magento 联系人 Controller

php - Magento 按管理顺序加载类别

ios - 允许访问 Health : how can I know in my app that the user enabled/disabled it later?

swift - selector为静态方法时如何设置NotificationCenter的观察者

php - Codeigniter 自定义回调返回包裹在段落中的消息