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.
Ali
Hi there,
I’ve been using your Ajax Add to Cart script, with great success so far, so thank you for that. What I’m trying to do now though is fire an event from within the addToCartTest.php file that a module can listen to (it’s the PRWD Autoshipping Module). But it looks like
MAGE::dispatchEvent...doesn’t work in external files. Any ideas?Cheers
December 8, 2010 at 11:03 am
Robert Nicklin
I’ve never needed to dispatch a Magento event from an external script. An immediate solution that comes to mind would be to create a module within your actual Magento application – and then reference that module from your external script to dispatch the event. Simply put, your external script won’t be dispatching the event, your module within your Magento application will be dispatching the event. Hope that helps!
August 11, 2011 at 4:24 pm
Nahid
Very simple way of explanation. good stuff.
Thanks.
June 16, 2011 at 11:31 pm