Magento Expert Forum - Improve your Magento experience

Results 1 to 2 of 2

Magento event observer - introduce and practice

  1. #1
    Junior Member ccvv's Avatar
    Join Date
    Mar 2013
    Thanked 17 Times in 13 Posts

    Post Magento event observer - introduce and practice

    Magento’s Event observer concept is about raising events in key areas in a 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

    HTML Code:
    <?xml version="1.0"?>
    Step 3: Next step is to create our config.xml file and place it in app/code/local/Mydons/Eventdemo/etc folder

    HTML Code:
    <?xml version="1.0"?>
    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.

    PHP Code:

    class Mydons_Eventdemo_Model_Observer {
    public function 
    Mytestmethod($observer) {
    $event $observer->getEvent(); //Fetches the current event
    $product $event->getProduct();
    $eventmsg “Current Event Triggered ” $event->getName() . 
    Currently Added Product 
    ” $product->getName();
    //Adds Custom message to shopping cart
    echo Mage::getSingleton(‘checkout/session’)->addSuccess($eventmsg);
    //Your Custom Logic Here
    //you can use print_r($product) here to get more details
    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 Magento Events Cheatsheet

    Example Source code can be downloaded with link below:

    View more threads in the same category:

  2. #2
    New member
    Join Date
    May 2013
    Thanked 0 Times in 0 Posts


    Nice post,thanks for sharing this information .

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts