将数据从 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');
}
享受并玩得开心!
相关文章