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
    Posts
    64
    Thanks
    6
    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"?>
         <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

    HTML Code:
    <?xml version="1.0"?>
          <config>
            <modules>
              <Mydons_Eventdemo>
              <version>0.1.0</version>
              </Mydons_Eventdemo>
            </modules>
            <frontend>
             <events>
                <checkout_cart_product_add_after>
                    <observers>
                       <Mydons_Eventdemo_Model_Observer>
                          <type>singleton</type>
                          <class>Mydons_Eventdemo_Model_Observer</class>
                          <method>Mytestmethod</method>
                       </Mydons_Eventdemo_Model_Observer>
                   </observers>
                </checkout_cart_product_add_after>
            </events>
          </frontend>
         </config>
    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:
    <?php

    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 1.4.2.0 Events Cheatsheet

    Example Source code can be downloaded with link below:

    magentoeventobserver.zip

    View more threads in the same category:


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

    Default

    Nice post,thanks for sharing this information .

Tags for this Thread

Bookmarks

Posting Permissions

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