Home » Magento22 February 2011

Magento: Event Observer with Save before and Save after

Magento has this cool functionality of Event Observer. Magento follows the Observer Design Pattern for its event hooking system. With this Event Observer Hook, additional functionality can be added without modifying the core code.

The full list of Magento Event Observer Hooks is present over here:- Magento Event/Observer Hooks Cheat Sheet

Magento has been programmed to raise events in crucial areas of the flow and handling these events for customizations would keep upgradation a simple task that does not require fiddling around with Magento’s core source code.

Event is something that occurs in a certain place during a particular sequence flow.

Observer is the handler of an event. This listens to any event it is attached to and accordingly handles the event.

More description and example on Event Observer and it’s difference with controller override is given over here:- Customizing Magento using Event-Observer Method

From the link just above, you can find example on how you can create and use event observer in Magento.

In this article, I am going to show you how you can use event observer before and after any events in Magento. An example would be the event ‘catalog_product_save_before‘ which will be raised by Magento before a product is saved. Similarly, there is an event ‘catalog_product_save_after‘ is raised by Magento immediately after a product is saved.

Here, we can add our custom event before and after any event raised by Magento’s core. In this way, we don’t have to use the dispatched events.

This way of adding custom event before and after save is useful when we just need to add or do something before or after any Magento core event. For example, setting some values to some attributes before or after product is saved.

For this, you just need to add add some event observer code in your module’s YourModule/etc/config.xml file and add a new observer YourModule/Model/Observer.php

In the example code below, I have added before and after event hook for customer address and catalog product. Customer address event hooks run before and after saving customer address and catalog product hooks run before and after saving products in admin.

customer_address_save_before = runs just before saving customer address
customer_address_save_after = runs immediately after saving customer address

customer_save_before = runs just before saving customer account information
customer_save_after = runs immediately after saving customer account information

catalog_product_save_before = runs just before saving product
catalog_product_save_after = runs just before saving product

Here’s the code:-


NOTE: We have written the event observer code inside global node. This will run the event observer code for both frontend and admin. If you need to run the event observer only in the frontend then you need to write the event observer code inside frontend node. Similarly, if you need to run the event observer only in the admin then you need to write the event observer code inside adminhtml node.


Similarly, you can add your custom function to other events like sales_order_save_before, sales_order_save_after, sales_order_invoice_save_before, sales_order_invoice_save_after, etc.

Hope this helps. Thanks.


Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • Thanks a lot! :)

  • Hi,

    After add to cart product in shopping cart which observer will be call?


  • Shazad Khan


    I have a problem.
    I want to change the group id of my customer when he change his country of shipping in his address. I have developped the module correctly with “customer_address_save_after” event.

    Then I change the CUSTOMER OBJET calling a setData(“group_id”, 4) in the address observer.

    In the debug, everything is working fine, i can see the change process before and after the setdata, but when the process finish, the group_id is the same as before….
    Is this because i’m not working on the customer but on the address ?
    How can I fix the problem and correctly save the customer changes ?

  • sunelsaminathan T

    catalog_product_save_before is not working

  • jimmyhien007


  • andy

    Thanks so much for this, you are a credit to open source software and the Magento community.

  • Pranay Gondane

    Just a typo in line “catalog_product_save_after = runs just before saving product”. It should be “catalog_product_save_after = runs just after saved product” :)

  • refaelgold

    very helpul!! thanks dude

  • Tried using the customer_address_save_before and customer_address_save_after in magento CE 1.7 but its not working. Any idea.

  • agiorgini

    using 1.9 here and not working. Need catalog_product_save before, why the config “class” attribute is different from YourNamespace_YourModule_Model_Observer? and in this case, how should be the complete class name in observer?