2

I need to execute my observer just after an actionController end or when paymentMethod is saved on database.

1 Answer 1

8

You can just use controller_action_postdispatch_<router_name>_<controller_name>_<action_name> formula to derive the event name. You can also do this for predispatch aswell (Just replace postdispatch with predispatch).

Core Example:

Observing Magento Checkout Onepage controller saveOrderAction on the Paypal module:

Mage_Checkout_OnepageController::saveOrderAction

<router_name>: checkout
<controller_name>: onepage
<action_name>: saveOrder

Using the previous formula to create the event name to observe we finally end up with:

Derived event name for our controller action method:

controller_action_postdispatch_checkout_onepage_saveOrder

Then in the etc/config.xml

<events>
    <controller_action_postdispatch_checkout_onepage_saveOrder>
        <observers>
             <hss_save_order_onepage>
                 <class>paypal/observer</class>
                 <method>setResponseAfterSaveOrder</method>
             </hss_save_order_onepage>
         </observers>
    </controller_action_postdispatch_checkout_onepage_saveOrder>
</events>

Getting the controller action in the Observer class:

/**
 * @param Varien_Event_Observer $observer
 */
public function setResponseAfterSaveOrder(Varien_Event_Observer $observer)
{
    /* @var $controller Mage_Core_Controller_Varien_Action */
    $controller = $observer->getEvent()->getData('controller_action');

    // Do stuff here
}
1
  • 1
    When controller is not Mage, Router Name should include extension vendor. Eg. IWD_Opc_JsonController should be "controller_action_postdispatch_iwd_opc_json_saveBillingAddress" not as implied "controller_action_postdispatch_opc_json_saveBillingAddress"
    – Flipmedia
    Commented Aug 1, 2018 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.