【问题标题】:What is the correct way to set up an observer in Magento?在 Magento 中设置观察者的正确方法是什么?
【发布时间】:2011-01-04 03:25:44
【问题描述】:

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

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

【问题讨论】:

    标签: php zend-framework magento


    【解决方案1】:

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

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

    在模块的 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
    

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

    谢谢, 乔

    【讨论】:

    • 事实是,我以不同的方式解决了我的问题。不过这对我来说是正确的。
    猜你喜欢
    • 2022-01-10
    • 2011-06-11
    • 1970-01-01
    • 2014-07-15
    • 1970-01-01
    • 2014-09-24
    • 1970-01-01
    • 2012-02-03
    • 1970-01-01
    相关资源
    最近更新 更多