在 Magento 中创建 Hello world 事件调度程序(观察者)?

发布于 2024-11-02 09:54:54 字数 545 浏览 1 评论 0原文

我正在尝试在 Magento 中创建一个事件调度程序(应在产品添加到购物车后调用)。

到目前为止我所做的:

我用谷歌搜索了这个并找到 链接。我按照此链接中指定的说明进行操作。我配置了 config.xml 文件并在 mymodule/model 文件夹下创建了一个 Observer.php 文件。但是,我看不到任何结果。

(我在示例中使用的 config.xml 和 Observer.php 文件的代码可以在 http://goo.gl 下找到/O7dBy,我的自定义模块名称是 Crossdata,包名称是 MyModule - 我做错了吗?)

任何其他包含简单 helloworld 事件调度程序示例的链接都会有所帮助。

谢谢, 巴兰

I am trying to create an event dispatcher in Magento (which should be called after a product gets added to cart).

What I've done so far:

I Googled this and found this link. I followed the instructions specified in this link. I configured the config.xml file and created a Observer.php filder under mymodule/model folder. But, I can't see any result from this.

(The code for config.xml and Observer.php file that i used in my example can be found under http://goo.gl/O7dBy, my custom module name is Crossdata and package name is MyModule - am i doing it wrong?)

Any other link with simple helloworld event dispatcher example would be helpful.

Thanks,
Balan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请止步禁区 2024-11-09 09:54:58
  1. 在您的config.xml中,为您的块和模型指定的短名称是crossdata。然而该事件使用别名dispatcher/observer,您尚未定义名称dispatcher。这可能需要是 crossdata/observer

  2. Inchoo 示例调用其模块 Inchoo_Dispatcher,模型为 Inchoo_Dispatcher_Model_Observer。您的模块是 MyPackage_Crossdata,这意味着别名 crossdata/observer 将尝试加载 MyPackage_Crossdata_Model_Observer。但您的观察者被错误地称为 MyPackage_Crossdata_Dispatcher_Model_Observer

  3. 如果您粘贴的代码没有显示在 Stack Overflow 中,请将其缩进四个空格或使用 {} 按钮对其进行格式化。这是显示代码的最佳方式。

  1. In your config.xml the short name given to your blocks and models is crossdata. Yet the event uses the alias dispatcher/observer, you have not defined a name dispatcher. This probably needs to be crossdata/observer.

  2. The Inchoo example calls it's module Inchoo_Dispatcher and the model is Inchoo_Dispatcher_Model_Observer. Your module is MyPackage_Crossdata which means the alias crossdata/observer would try to load MyPackage_Crossdata_Model_Observer. But your observer is erroneously called MyPackage_Crossdata_Dispatcher_Model_Observer.

  3. If your pasted code is not showing here in Stack Overflow either indent it by four spaces or use the {} button to format it. That is the preferable way to show code.

我也只是我 2024-11-09 09:54:57

查看从 http://goo.gl/O7dBy 下载的代码,您定义:

<events>
    <add_to_cart_after>
        <observers>
            <add_to_cart_after>
                <class>dispatcher/observer</class>
                <method>hookToAddToCartAfter</method>
            </add_to_cart_after>
        </observers>
    </add_to_cart_after>
</events>

并定义 < 的模型别名code>dispatcher as:

<models>
    <dispatcher>
        <class>MyPackage_Crossdata_Dispatcher_Model</class>
    </dispatcher>
</models>

生成的类名称为 MyPackage_Crossdata_Model_Dispatcher_Model_Observer,但您的观察者类被命名为MyPackage_Crossdata_Dispatcher_Model_Observer,缺少 CrossdataDispatcher 之间的 Model

我建议删除上面的整个 dispatcher 模型定义,只使用您已在 config.xmlcrossdata >:

<crossdata>
    <class>MyPackage_Crossdata_Model</class>
    <resourceModel>crossdata_mysql4</resourceModel>
</crossdata>   

然后将事件定义更改为:

<add_to_cart_after>
    <class>crossdata/observer</class>
    <method>hookToAddToCartAfter</method>
</add_to_cart_after>

创建一个文件 /app/code/local/MyPackage/Crossdata/Model/Observer.php 并在其中定义您的观察者类:

class MyPackage_Crossdata_Model_Observer
{
    public function hookToAddToCartAfter($observer)
    {
        // your observer code
    }
}

Looking at your code downloaded from http://goo.gl/O7dBy you define:

<events>
    <add_to_cart_after>
        <observers>
            <add_to_cart_after>
                <class>dispatcher/observer</class>
                <method>hookToAddToCartAfter</method>
            </add_to_cart_after>
        </observers>
    </add_to_cart_after>
</events>

and define the model alias of dispatcher as:

<models>
    <dispatcher>
        <class>MyPackage_Crossdata_Dispatcher_Model</class>
    </dispatcher>
</models>

The resulting class name would be MyPackage_Crossdata_Model_Dispatcher_Model_Observer, but your observer class is named MyPackage_Crossdata_Dispatcher_Model_Observer, missing the Model in between Crossdata and Dispatcher.

I'd recommend to drop the whole dispatcher models definition above and only use the other model alias crossdata which you've already defined in config.xml:

<crossdata>
    <class>MyPackage_Crossdata_Model</class>
    <resourceModel>crossdata_mysql4</resourceModel>
</crossdata>   

Then change the event definition to:

<add_to_cart_after>
    <class>crossdata/observer</class>
    <method>hookToAddToCartAfter</method>
</add_to_cart_after>

Create a file /app/code/local/MyPackage/Crossdata/Model/Observer.php and define your observer class in it:

class MyPackage_Crossdata_Model_Observer
{
    public function hookToAddToCartAfter($observer)
    {
        // your observer code
    }
}
成熟的代价 2024-11-09 09:54:57

您添加了 Observer.php 的文件夹,还是文件?您可以发布为此编写的代码,以便我们更好地帮助您进行调试吗?该教程非常简单明了。

You added a folder for Observer.php, or a file? Can you post the code you wrote for this so we can better help debug with you? That tutorial is about as simple and straightforward as they get.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文