What is the correct way to set up an observer in Magento?(在 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
实际上,您可以在观察者中随意命名方法,但模式似乎是将类本身命名为观察者.它使用普通模型加载(例如 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 中设置观察者的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Magento 中设置观察者的正确方法是什么?


基础教程推荐
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何替换eregi() 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01