将数据从 Magento 中的事件观察器返回给调度程序

Return data back to dispatcher from event observer in Magento(将数据从 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 中的事件观察器返回给调度程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)