Return data back to dispatcher from event observer in Magento(将数据从 Magento 中的事件观察器返回给调度程序)
问题描述
我有一个产品注册扩展,它在注册保存后调度一个事件.如果虚拟产品与注册产品相关,另一个扩展程序使用该事件为虚拟产品生成优惠券.
I have an extension for product registration that dispatches an event after the registration is saved. Another extension uses that event to generate a coupon for a virtual product if it is related to the registered product.
我需要取回有关生成的优惠券的数据,以便通过电子邮件将其连同产品注册的详细信息一起发送给用户.
I need to get back data on the generated coupon to send to the user in an email along with the details of their product registration.
有没有办法将数据从观察者返回到事件被调度的地方?
Is there a way to return data from the observer back to where the event is dispatched?
推荐答案
Magento 中有一个技巧可用于您的目的.由于您可以将事件数据(如产品或类别模型)传递给观察者,因此还可以创建一个容器,从中获取这些数据.
There is a trick available in Magento for your purpose. Since you can pass event data to the observers, like product or category model, it also possible to create a container from which you can get this data.
例如,可以在调度程序中执行此类操作:
For instance such actions can be performed in dispatcher:
$couponContainer = new Varien_Object();
Mage::dispatchEvent('event_name', array('coupon_container' => $couponContainer));
if ($couponContainer->getCode()) {
// If some data was set by observer...
}
观察者方法如下所示:
public function observerName(Varien_Event_Observer $observer)
{
$couponContainer = $observer->getEvent()->getCouponContainer();
$couponContainer->setCode('some_coupon_code');
}
享受并玩得开心!
这篇关于将数据从 Magento 中的事件观察器返回给调度程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将数据从 Magento 中的事件观察器返回给调度程序
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- PHP - if 语句中的倒序 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
