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.
<?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
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
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.
<?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
i)
ii)
iii)
iv)
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)
vi)
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:
<?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; |
|
}
|
|
}
|
|
?>
|