在Magento中建立观察员的正确方法是什么? [英] What is the correct way to set up an observer in Magento?

查看:53
本文介绍了在Magento中建立观察员的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Magento中设置一个观察者,以便在订单状态发生变化时执行操作.

I'd like to set up an observer in Magento that performs an action when the status of an order changes.

我熟悉创建模块的过程.我想了解的是需要在模块config.xml中放置什么,以及需要创建的类和/或方法的命名约定是什么.

I'm familiar with process of creating modules. What I'm looking to understand is what needs to placed in the modules config.xml, and what is the naming convention for the classes and/or methods that need to be created.

推荐答案

我在任何地方都看不到事件名称,但是我将在此处发布一般情况:

I don't see the event name anywhere, but I'll post the general case here:

假设:您已经设置了一个模块,并且已从Yourmodule/Model目录正确加载了模型.

Assumed: That you have a module set up, with models being loaded correctly from the Yourmodule/Model directory..

在模块的config.xml文件中:

In your module's config.xml file:

<config>
    <global>
  <events>
   <full_event_name>
    <observers>
     <yourmodule>
      <type>singleton</type>
      <class>yourmodule/observer</class>
      <method>yourMethodName</method>
     </yourmodule>
    </observers>
   </full_event_name>
  </events>
 </global>
</config>

使用以下内容创建文件%yourmodule%/Model/Observer.php:

Create a file %yourmodule%/Model/Observer.php with the following contents:

<?php

class Yourmodule_Model_Observer {

    public function yourMethodName($event) {
        $data = $event->getData(); // this follows normal Magento data access

        // perform your action here
    }

}//class Yourmodule_Model_Observer

确实,您可以在观察者中随意命名该方法,但是这种模式似乎是将类本身命名为Observer.使用常规模型加载来加载它(例如yourmodule/observer映射到Yourmodule_Model_Observer).希望有帮助!

Really, you can name the method whatever you want within your observer, but the pattern seems to be to name the class itself Observer. It is loaded using normal model loading (e.g. yourmodule/observer maps to Yourmodule_Model_Observer). Hope that helps!

谢谢, 乔

这篇关于在Magento中建立观察员的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆