Latching onto magento events is very simple – however, after a lot of searching I couldn’t find a lot of resources documenting how to do it.
I’ll more than likely research magento events a bit more in a later post, but for reference here is a cheatsheet of dispatched magento events Credit: Branko Ajzele of Active Codeline. (a little outdated – from version 1.3)
Let’s keep it simple…
The namespace for my module will be “Ifuel”
The name of my module will be “Aftercheckout”
I will want to latch onto one of the many dispatched events during the checkout process (to name a few: checkout_controller_onepage_save_shipping_method, sales_order_place_after, checkout_type_onepage_save_order_after, checkout_onepage_controller_success_action). For reference about checkout events, check out Yireo’s Events with Magento Checkout. For this module, I want to execute my class after the order has been completely checked out – so I will latch onto the “checkout_onepage_controller_success_action” event.
Here is the bare bones of my config.xml file located at /app/code/local/Ifuel/Aftercheckout/etc/config.xml
. Make sure to read the comments in the code, they’re helpful. P.s. don’t forget to declare your module in /app/etc/modules/
<?xml version="1.0"?> <config> <modules> <Ifuel_Aftercheckout> <version>0.1.0</version> </Ifuel_Aftercheckout> </modules> <frontend> <routers> <aftercheckout> <use>standard</use> <args> <module>Ifuel_Aftercheckout</module> <frontName>aftercheckout</frontName> </args> </aftercheckout> </routers> </frontend> <global> <events> <checkout_onepage_controller_success_action> <!-- The name of the event you're latching onto, all lowercase --> <observers> <sendaftercheckout> <!-- Some description for your event listener, doesn't matter what it is as long as there are no spaces and it's all lowercase --> <type>singleton</type> <class>aftercheckout/doaftercheckout</class> <!-- the <frontName> of your module (aftercheckout) slash the name of your Event Listener (doaftercheckout), case sensitive. In this case it will be located at /app/code/local/Ifuel/Aftercheckout/Model /Doaftercheckout.php --> <method>sendSomethingAfterCheckout</method> <!-- the public function name within your class that will listen for this event, case sensitive --> </sendaftercheckout> </observers> </checkout_onepage_controller_success_action> </events> </global> </config>
Now you’ll want to create your Listener. In my case it will be located at /app/code/local/Ifuel/Aftercheckout/Model/Doaftercheckout.php. Make sure to create a public function within your new class that you’ve defined in the <method> node of your config.xml file. The contents of my listener will look something like:
<?php class Ifuel_Aftercheckout_Model_Doaftercheckout { public function sendSomethingAfterCheckout() { Mage::log("I am now able to execute something after checkout"); } }
And that’s it. That’s the bare bones of latching onto a magento event. It’s that simple. If you have any questions, feel free to ask them in a comment.