Friday , April 26 2024
Home / Magento / How to create an observer in Magento?

How to create an observer in Magento?

How to create an observer in Magento.?
—————————–
We are consider that you are familiar to create new module in magento.
Following are the steps to create a module in magento to override the observer.

Step 1) Create main module xml file

We need to create a file called Cmpny_Observername.xml in app/etc/modules. where “Cmpny” is represent namespace and “Observername” is module name and underscore(_) indicate the pattern to create module structure.
Below are the xml structure.

Select Code
<?xml version="1.0"?>
<config>
    <modules>
        <Cmpny_Observername>
            <codePool>local</codePool>
            <active>true</active>
        </Cmpny_Observername>
    </modules>
</config>


Magento mainly used three code pool “community”, “core”, “local” and have facility to extend more code pools. this node local indicate that you need to place module code inside the “local” codePool.

Cmpny_Observername.xml file used for installing module into magento and give the facility to activate and deactivate this module by changing line of code true to false.

Step 2) Create directory structure for our module

We need to create the directory where our modules code will need to copy. To do this create the following directories:

mkdir app/code/local/Cmpny/
mkdir app/code/local/Cmpny/Observername/
mkdir app/code/local/Cmpny/Observername/etc
mkdir app/code/local/Cmpny/Observername/Model

The module directory structure show the clear picture about the files where we need to paste it. the directory inside the Module name “etc” will contain various configuration files which instructs Magento how to read/use this module and “Model” contains various Model classes for actual operation or functionality of the module.

Step 2) Create our module’s configuration file

Each module requires a file called config.xml this file paste inside the “app/code/local/Cmpny/Observername/etc”
and below are the xml structure of config xml file.

Select Code
<?xml version="1.0"?>
<config>
    <modules>
        <Cmpny_Observername>
            <version>0.0.1</version>
        </Cmpny_Observername>
    </modules>
    <global>
        <models>
            <CmpnyObservername>
                <class>Observername_Model</class>
            </CmpnyObservername>
        </models>
        <events>
            <OBSERVER_ACTION_EVENT_NAME>
                <observers>
                    <OBSERVER_MODEL_NAME>
                        <type>singleton</type>
                        <class>Cmpny_Observername_Model_Observer</class>
                        <method>YOUR_METHOD_NAME</method>
                    </OBSERVER_MODEL_NAME>
                </observers>
            </OBSERVER_ACTION_EVENT_NAME>
        </events>
    </global>
</config>


The above code is our basic config for our model.
Below we are explaing about Node use in our modules.
i) you can find the event list here “http://www.magentocommerce.com/wiki/5_-_modules_and_development/reference/magento_events” to override any event in the column “Event Name”
ii) tag is used for grouping obsever name , type, class and method.
iii) Used for giving the observer model name.
iv) key in the configuration. the content is ‘singleton’ and is dealt with in the dispatchEvent method of class Mage_Core_Model_App, method dispatchEvent, which is executed from class Mage. You use the types: ‘disabled’, ‘model’ or ‘singleton’.
Type ‘disabled’: the observer class is not instantiated and the observer method is not executed.
Type ‘model’: a new instance of the observer class is instantiated for each event and the observer method of that instance is executed.
Type ‘singleton’: a single instance of the observer class is instantiated and the observer method of that single instance is executed for each event.
v) represents the class name of our Observer
vi) is the method which is actually fired at the time of event occurs.

Step 4) Creating our Observer.php

Now create a file named Observer.php in paste it inside the “app/code/local/Cmpny/Observername/Model” and place the following code:

Select Code
<?php
class Cmpny_Observername_Model_Observer {

    public function YOUR_METHOD_NAME($observer) { 
            $quote = $observer->getEvent();
            //Place here you functionality of observer and then return it.
            return $this;
    }

}
?>

About v.shakya

I am V.Shakya, Software Developer & Consultant I like to share my ideas, views and knowledge to all of you who come across my website. I am young, enthusiastic, highly motivated and self disciplined person. I completed my studies in Master of Computer Application and currently giving my technical expertise to one of the Big IT company. I have more than fifteen years of experience in vast field of Programming , Designing and Development of websites and various software's.

Check Also

How we can add one more new option in product images in magento?

You would need to update following files : 1) appcodecoreMageCatalogModelProductAttributeBackendMedia.php 2) appcodecoreMageCatalogModelResourceProductAttributeBackendMedia.php 3) appcodecoreMageCatalogModelProductAttributeMediaApi.php 4) …

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.