在 Magento 中创建 Hello world 事件调度程序(观察者)?
我正在尝试在 Magento 中创建一个事件调度程序(应在产品添加到购物车后调用)。
到目前为止我所做的:
我用谷歌搜索了这个并找到 此链接。我按照此链接中指定的说明进行操作。我配置了 config.xml 文件并在 mymodule/model 文件夹下创建了一个 Observer.php 文件。但是,我看不到任何结果。
(我在示例中使用的 config.xml 和 Observer.php 文件的代码可以在 http://goo.gl 下找到/O7dBy,我的自定义模块名称是 Crossdata,包名称是 MyModule - 我做错了吗?)
任何其他包含简单 helloworld 事件调度程序示例的链接都会有所帮助。
谢谢, 巴兰
I am trying to create an event dispatcher in Magento (which should be called after a product gets added to cart).
What I've done so far:
I Googled this and found this link. I followed the instructions specified in this link. I configured the config.xml file and created a Observer.php filder under mymodule/model folder. But, I can't see any result from this.
(The code for config.xml and Observer.php file that i used in my example can be found under http://goo.gl/O7dBy, my custom module name is Crossdata and package name is MyModule - am i doing it wrong?)
Any other link with simple helloworld event dispatcher example would be helpful.
Thanks,
Balan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的
config.xml
中,为您的块和模型指定的短名称是crossdata
。然而该事件使用别名dispatcher/observer
,您尚未定义名称dispatcher
。这可能需要是crossdata/observer
。Inchoo 示例调用其模块
Inchoo_Dispatcher
,模型为Inchoo_Dispatcher_Model_Observer
。您的模块是MyPackage_Crossdata
,这意味着别名crossdata/observer
将尝试加载MyPackage_Crossdata_Model_Observer
。但您的观察者被错误地称为MyPackage_Crossdata_Dispatcher_Model_Observer
。如果您粘贴的代码没有显示在 Stack Overflow 中,请将其缩进四个空格或使用 {} 按钮对其进行格式化。这是显示代码的最佳方式。
In your
config.xml
the short name given to your blocks and models iscrossdata
. Yet the event uses the aliasdispatcher/observer
, you have not defined a namedispatcher
. This probably needs to becrossdata/observer
.The Inchoo example calls it's module
Inchoo_Dispatcher
and the model isInchoo_Dispatcher_Model_Observer
. Your module isMyPackage_Crossdata
which means the aliascrossdata/observer
would try to loadMyPackage_Crossdata_Model_Observer
. But your observer is erroneously calledMyPackage_Crossdata_Dispatcher_Model_Observer
.If your pasted code is not showing here in Stack Overflow either indent it by four spaces or use the {} button to format it. That is the preferable way to show code.
查看从 http://goo.gl/O7dBy 下载的代码,您定义:
并定义 < 的模型别名code>dispatcher as:
生成的类名称为
MyPackage_Crossdata_Model_Dispatcher_Model_Observer
,但您的观察者类被命名为MyPackage_Crossdata_Dispatcher_Model_Observer
,缺少Crossdata
和Dispatcher
之间的Model
。我建议删除上面的整个
dispatcher
模型定义,只使用您已在config.xml
crossdata >:然后将事件定义更改为:
创建一个文件
/app/code/local/MyPackage/Crossdata/Model/Observer.php
并在其中定义您的观察者类:Looking at your code downloaded from http://goo.gl/O7dBy you define:
and define the model alias of
dispatcher
as:The resulting class name would be
MyPackage_Crossdata_Model_Dispatcher_Model_Observer
, but your observer class is namedMyPackage_Crossdata_Dispatcher_Model_Observer
, missing theModel
in betweenCrossdata
andDispatcher
.I'd recommend to drop the whole
dispatcher
models definition above and only use the other model aliascrossdata
which you've already defined inconfig.xml
:Then change the event definition to:
Create a file
/app/code/local/MyPackage/Crossdata/Model/Observer.php
and define your observer class in it:您添加了 Observer.php 的文件夹,还是文件?您可以发布为此编写的代码,以便我们更好地帮助您进行调试吗?该教程非常简单明了。
You added a folder for Observer.php, or a file? Can you post the code you wrote for this so we can better help debug with you? That tutorial is about as simple and straightforward as they get.