Simple example using magento event observer

Magento’s Event observer concept is about raising events in key areas in an application flow. An Event is an action that takes place during normal program execution. Eg: Product saved,order placed,etc.

Observer is the event handler. Customizing Magento using Event observer allows us to add our own logic in addition to the core logic without changing any core code.

The Sample Module illustrated here will trigger an event whenever a new product is added to the shopping cart. Here we will write our own observer (event handler) to handle this event. Our observer
will display the currently triggered event name and product name along with the success message.

Step 1: Create the below folders in app/code/local/Mydons/Eventdemo/etc,app/code/local/Mydons/Eventdemo/Model. In this example Mydons is the companyname and Eventdemo is the module name.You can use your own companyname and modulename.

Step 2: Lets create the module activation file Mydons_Eventdemo.xml and place it in app/etc/modules/ directory

<?xml version="1.0"?> <config> <modules> <Mydons_Eventdemo> <active>true</active> <codePool>local</codePool> </Mydons_Eventdemo> </modules> </config>

Step 3: Next step is to create our config.xml file and place it in app/code/local/Mydons/Eventdemo/etc folder

Step 4: Finally we are going to create our Model class file observer.php. Save the Model file in app/code/local/Mydons/Eventdemo/Model/ folder.

Step 5: Thats it we finished our module. Now go to frontend and add any product to the shopping cart. Our observer will be called.
In addition to the default success message our custom modules message will be added.
If you need the list of magento events as of 1.4.2 Please visit the below site
Magento 1.4.2.0 Events Cheatsheet

Requirement :- Magento
Download :-http://mydons.com/download/75b5411e5c83ea7e9a8f9f80d0746f21/magentoeventobserver.zip
License :- License Free

  1. Iqbal says:

    i was looking for this solution for my extension that how to apply hook in magento this will help me alot
    Thanks man..

    good luck..

  2. vashishtha says:

    thanks a ton …! it helps me a lot….!

  3. prat says:

    HI,
    It’s really very informative & useful article. I wanted to know more about event-observer model of Magento.
    Currently I am focussing on sales order placement event. I have created one custom table in Magento that contains different sales order related columns(like increment id from sales_flat_order, item_name,sku from sales_flat_order_item etc.). I connected to mysql database through PHP. I join these table to form a single query & put this query which fetches & inserts data into custom table.
    Now my ultimate aim is once customer places order, this php file must get called so that all data fetched through that query get inserted into custom table…
    So how event-observer model I can use for the same…do u have any sample code for the same…
    Plz guide me…

    Regards,
    Prat

    • Ramesh says:

      Hi Prat, Thanks for your feedback. To handle sales order placement event, you must first know about the list of events supported
      in your Magento version. I think for Version 1.5.1.0 checkout_onepage_controller_success_action is the event that will be
      triggered once order is placed in frontend. You need to handle this event in a Model class named Observer.php, in this class you need to
      implement your custom PHP code. For Further clarification you can refer the official magento site or google search.

  4. bilyana says:

    Hi Ramesh.
    thank you so much for this article, it helps a lot in order to understand how all this works. i followed it and worked perfectly.
    I’m trying to let customers upload a file at customer registration, do you know which event is trigerred and how to do that.
    and maybe in general, how do you find which event is triggered at some action or another in magento?
    thanks a lot.

    • Ramesh says:

      Hi bilyana, iam glad to hear that you understand how this stuff works. To get a list of events triggered i have given a link at the end of the article Magento 1.4.2.0 Events Cheatsheet. This site contains the cheatsheet of events for latest version also.

  5. bilyana says:

    Hi Ramesh,
    thanks for your reply. I followed exactly the same steps you describe and tried to display the name of the event triggered at customer registration using the customer_register_success event and the code you gave but the message doesn’t get displayed not the name of the file from the array $_FILES[‘prooffile’][‘name’] from my customer registration page, even though i have set up the enctype=”multipart/form-data”.
    I see that you understand this part quite well and i was wondering if you have a hint of what i’m doing wrong??
    thanks a lot

    • Ramesh says:

      Hi Bilyana have u cleared the cache, please check if you have missed anything in your config.xml Are you observing the right event, if you are uploading your file on customer registration form then the customer controller code must be overriden to handle file handling. Observer function will be called and executed only after passing the customer controller. I dont think it will directly handle files uploaded.

  6. fer says:

    Hello, do you know what’s the function to get a form on a save_after event? i tried $observer->getEvent()->getRequest()->getPost() and its empty, tried $this->getRequest()->getPost() also tried Mage::app()->getRequest()->getPost() and its empty

  7. fer says:

    nevermind, if anyone wants to do that, try Mage::app()->getRequest()->getPost(); it returns an array with all the form data, you can use as you please. This is for admin forms, with normal phtml forms, i guess you could use $this

  8. Jamie says:

    Hi Ramesh,

    Thanks for the tutorial, i’m just getting started with Magento and this article was very helpful in getting an understanding of how to create modules.

    I stumbled across the article which researching the best method to update configurable product prices based on options and i’m wondering if you would recommend a similar method to achieve my goal? For example, i’d check the selected product options (width, height etc) and set the item price accordingly as it’s added to the cart.

    Thanks!

  9. LeonardoCM87 says:

    works in Magento 1.6.1… Thanks 😀

  10. Ezri says:

    Now this is gow Magento should be teached! Thanks for the example!

Leave a Comment

Your email address will not be published. Required fields are marked *

8 − 1 =