代码之家  ›  专栏  ›  技术社区  ›  Laizer

在Magento中设置观察者的正确方法是什么?

  •  1
  • Laizer  · 技术社区  · 15 年前

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

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

    1 回复  |  直到 15 年前
        1
  •  4
  •   Joe Mastey    15 年前

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

    在模块的config.xml文件中:

    <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:

    <?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