Create a Module to Latch onto Magento Event

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/
[sourcecode]




0.1.0





standard

Ifuel_Aftercheckout
aftercheckout











singleton
aftercheckout/doaftercheckout

sendSomethingAfterCheckout







[/sourcecode]

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:

[sourcecode]

5 Responses to “Create a Module to Latch onto Magento Event”

  1. 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 🙂

    • 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!

  2. I followed the same procedure but i am getting the following error ..please help me out ..
    Warning: include(Tweett_SalesRule_Model_Specialobserver.php) [function.include]: failed to open stream: No such file or directory in /home/tweet/public_html/includes/src/Varien_Autoload.php on line 93

    app/code/local/Tweett/SalesRule/etc/config.xml file is
    ——————————–

    0.1.0

    standard

    Tweett_SalesRule
    salesrule

    singleton
    salesrule/specialobserver
    newCouponDiscountCal

    And the app/code/local/Tweett/SalesRule/Model/Specialobserver.php
    ——————————————-
    <?php
    class Tweett_SalesRule_Model_Specialobserver
    {
    public function newCouponDiscountCal($observer)
    {
    echo "Hello World .. “;
    $item=$observer[‘item’];
    $_product = Mage::getResourceSingleton(‘catalog/product’)->load($item->getProductId());
    if($_product->getSpecialPrice()>0 ) {

    $result = $observer[‘result’];
    $result->setDiscountAmount(0);
    }
    }
    }
    ?>

  3. i think the editor didn’t pick up the tags in xml file .any other way so that i can share my config.xml file with you ..
    its been a 10-12 days i am messing with this issue

    Thanks ..

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>