1

We use Onepage checkout in Magento 1.4

The file where the work is done is:

/app/code/core/Mage/Checkout/controllers/OnepageController.php

The function is saveOrderAction()

I want to perform an action after the order has been saved, but a) I don't see any dispatchEvent call in this function and b) I don't know which event to use or to search for.

I have seen exhaustive event lists for Mage 1.x, but I don't know how to correlate to this method.

I also need to know how to add the <observer> XML and place the function, as I have never done this before.

2 Answers 2

4

Hi you can use sales_order_place_after event.you need to define below code in your module config.xml.

 <global>
     <events>
        <sales_order_place_after>
            <observers>
                <OBSERVER_NAME>
                    <class>MODULE_FRONTNAME/NAME_OF_FILE_IN_MODEL_FOLDER</class>
                    <method>FUNCTION_NAME_FROM_ABOVE_CLASS</method>
                </OBSERVER_NAME>
            </observers>
        </sales_order_place_after>
     </events>
 </global>

If you need to define file under following path: app/code/YOUR_CODEPOOL/NAMESPACE/YOUR_MODULE/Model/NAME_OF_FILE_IN_MODEL_FOLDER.php Like Observer.php

In above file need to create function in observer.php with name which define in tag of config.xml:

<?php
    public function sales_order_afterPlace($observer)
    {
      $order = $observer->getEvent()->getOrder();
      /*YOUR LOGIC*/
    }
?>

I hope this will help you.

1
  • perfect answer with explanation! +1 :) Commented Oct 2, 2017 at 10:35
1

Every single controller has a default generic event that can be used for observers. They follow the following naming convention:

  • Before dispatching the action: <controller_action_predispatch_<module>_<controller>_<action>>
  • After dispatching the action: <controller_action_postdispatch_<module>_<controller>_<action>>

In your case, it would be the following event: <controller_action_postdispatch_checkout_onepage_saveOrder>

Then in order to observer this specific event, you need to add the following code to your global tag of your config.xml file:

<controller_action_postdispatch_checkout_onepage_saveOrder>
            <observers>
                <unique_name_for_observer>
                    <type>singleton</type>
                    <class>module/observer</class>
                    <method>postDispatchSaveOrder</method>
                </unique_name_for_observer>
            </observers>
</controller_action_postdispatch_checkout_onepage_saveOrder>

This is assuming that your models are declared under module class name.

Then you need to create the Model/Observer.php file which would look like this:

<?php

class My_Module_Model_Observer
{
    public function postDispatchSaveOrder(Varien_Event_Observer $observer)
    {
        // Get the response
        $post = $observer->getControllerAction()->getResponse();
        // Do what you need to do with the response
    }
}

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.