1

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

Is there any simple solution?

Wakanina
  • 764
  • 2
  • 13
  • 25
  • That depends heavily on what you want to do... So what do you want to do?! ;) – mpaepper Jan 29 '13 at 09:28
  • I want to set cookie after user chose the shipping method (and click Continue) in the checkout page. What event is this? – Wakanina Jan 29 '13 at 09:35
  • 2
    We already have a question about how to get a list of all events: http://magento.stackexchange.com/questions/153/where-can-i-find-a-complete-list-of-magento-events

    That should help in finding the right event ;)

    – Tobias Jan 29 '13 at 09:35
  • Thank you Tobias, but actually I don't want a list. What I want is the method to find the right event. :) – Wakanina Jan 29 '13 at 09:41
  • See my answer on this question [http://magento.stackexchange.com/questions/176/whats-the-last-event-dispatched-before-content-is-sent-to-the-browser] (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 Jan 29 '13 at 14:28
  • dedmeet, thank you for your tips, this is what I'm looking for as well. I will try :) – Wakanina Jan 29 '13 at 16:36

2 Answers2

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/
Kenny
  • 1,395
  • 8
  • 19
  • @KennyDs very usefu tool thank u , i have put more details on http://magento.stackexchange.com/questions/19749/how-to-trace-events-alternatives-of-magedispatchevent-to-hook-events – Matoeil May 13 '14 at 16:24
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.

mpaepper
  • 4,700
  • 3
  • 23
  • 42
  • 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. – Mark Shust at M.academy Jan 29 '13 at 14:12