从Magento中的事件观察器将数据返回给调度程序 [英] Return data back to dispatcher from event observer in Magento

查看:44
本文介绍了从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中的事件观察器将数据返回给调度程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆