問題描述
我想在 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
實際上,您可以在觀察者中隨意命名方法,但模式似乎是將類本身命名為觀察者.它使用普通模型加載(例如 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 中設置觀察者的正確方法是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!