1

My question is simple, how to know the Magento event that we want to hook?

Is there any simple solution?

6
  • That depends heavily on what you want to do... So what do you want to do?! ;)
    – mpaepper
    Commented Jan 29, 2013 at 9:28
  • I want to set cookie after user chose the shipping method (and click Continue) in the checkout page. What event is this?
    – Wakanina
    Commented Jan 29, 2013 at 9:35
  • 2
    We already have a question about how to get a list of all events: magento.stackexchange.com/questions/153/… That should help in finding the right event ;)
    – Tobias
    Commented Jan 29, 2013 at 9:35
  • Thank you Tobias, but actually I don't want a list. What I want is the method to find the right event. :)
    – Wakanina
    Commented Jan 29, 2013 at 9:41
  • See my answer on this question [magento.stackexchange.com/questions/176/… (logging the events to system.log) This has served me well with the use of breakpoints to see what events fired up to that breakpoint. or without a breakpoint, all the events fired on an action.
    – ProxiBlue
    Commented Jan 29, 2013 at 14:28

2 Answers 2

14

If you want to see the list of events that Magento has you have 3 options:

1) Google for it, there are a lot of people who have compiled a list of Magento events

2) Create a module that hooks on the controller_action_predispatch event which is the event that is called before any other event is called. Inside this module you can log each event that is dispatched:

Add the following on config.xml

<events>
    <controller_action_postdispatch>
        <observers>
            <controller_action_after>
                <class>yourmodule/observer</class>
                <method>hookToControllerActionPostDispatch</method>
            </controller_action_after>
        </observers>
    </controller_action_postdispatch>
</events>

And inside the yourmodule/Model/Observer:

public function hookToControllerActionPostDispatch($observer) {
    Mage::log($observer->getEvent()->getControllerAction()->getFullActionName());
}

The above would log every event that is dispatched...

3) If you have SSH access you can run the following command to get an overview of all the events (and their files where they are dispatched):

cd /path/to/<magento-root>
grep -nris 'dispatchEvent' app/code/
1
5

The OnepageController dispatches an event for saving the shipping method. You could probably add your logic in there:

From OnepageController.php:

Mage::dispatchEvent('checkout_controller_onepage_save_shipping_method',
                    array('request'=>$this->getRequest(),
                        'quote'=>$this->getOnepage()->getQuote()));

So, your event name is checkout_controller_onepage_save_shipping_method.

1
  • 1
    I generally trace the action of where I want something dispatched back to the model or controller of where things are taking place. So in this case, you are on the One Page Checkout. So you can go to app/code/core/Mage/Checkout/controllers/OnepageController.php and then search for dispatchEvent in your shipping action as pointed out above. You will eventually know where to look Mage/Checkout, Mage/Customer, etc.) after some time with Magento. Best is to just keep looking until you find something. If a dispatchEvent doesn't exist, you can override that controller/model and add one in. Commented Jan 29, 2013 at 14:12

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