問(wèn)題描述
我有一個(gè)產(chǎn)品注冊(cè)擴(kuò)展,它在注冊(cè)保存后調(diào)度一個(gè)事件.如果虛擬產(chǎn)品與注冊(cè)產(chǎn)品相關(guān),另一個(gè)擴(kuò)展程序使用該事件為虛擬產(chǎn)品生成優(yōu)惠券.
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.
我需要取回有關(guān)生成的優(yōu)惠券的數(shù)據(jù),以便通過(guò)電子郵件將其連同產(chǎn)品注冊(cè)的詳細(xì)信息一起發(fā)送給用戶.
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.
有沒(méi)有辦法將數(shù)據(jù)從觀察者返回到事件被調(diào)度的地方?
Is there a way to return data from the observer back to where the event is dispatched?
推薦答案
Magento 中有一個(gè)技巧可用于您的目的.由于您可以將事件數(shù)據(jù)(如產(chǎn)品或類(lèi)別模型)傳遞給觀察者,因此還可以創(chuàng)建一個(gè)容器,從中獲取這些數(shù)據(jù).
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.
例如,可以在調(diào)度程序中執(zhí)行此類(lèi)操作:
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');
}
享受并玩得開(kāi)心!
這篇關(guān)于將數(shù)據(jù)從 Magento 中的事件觀察器返回給調(diào)度程序的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!