問題描述
我有一個產品注冊擴展,它在注冊保存后調度一個事件.如果虛擬產品與注冊產品相關,另一個擴展程序使用該事件為虛擬產品生成優惠券.
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 中的事件觀察器返回給調度程序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!