Skip to content
Advertisement

Return data back to dispatcher from event observer in 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?

Advertisement

Answer

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...
}

And an observer method can look like the following:

public function observerName(Varien_Event_Observer $observer) 
{
    $couponContainer = $observer->getEvent()->getCouponContainer();
    $couponContainer->setCode('some_coupon_code');
}

Enjoy and have fun!

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement