This article will demonstrate how to add a Magento observer where you have the ability to hook onto an in-built Magento event or onto a custom event of your own.
If you haven’t already read the Magento Dispatch Event article, it would be a good idea to familiarise yourself with Magento’s Event Driven Architecture.
Let’s start with the initial setup for a custom observer. We first need to create our own custom module, and therefore we need to create our module’s declaration file.
<?xml version="1.0"?> <config> <modules> <Custom_Observer> <codePool>local</codePool> <active>true</active> </Custom_Observer> </modules> </config>
We then need to add our config.xml, and define the configuration for our observer.
<?xml version="1.0"?> <config> <frontend> <events> <checkout_cart_add_product_complete> <observers> <custom_observer> <type>model</type> <class>custom_observer/observer</class> <method>someObserverMethod</method> </custom_observer> </checkout_cart_add_product_complete> </events> </frontend> </config>
What we’ve done here is added our own custom observer that hooks onto Magento’s checkout_cart_add_complete_product
event. We can hook onto other events by renaming the checkout_cart_add_complete_product
node to another of Magento’s events.
It’s also worth noting that the configuration has been added within the pair of frontend
nodes. Note that if you need to hook onto an event only fired in the admin area, then the observer configuration needs to added within a pair of admin
nodes. You can also add observer configuration within a pair of global
nodes, should you need to hook onto events fired in both the frontend and admin areas.
As the observer is typically a model that is added, we need to declare our model within config.xml
.
<?xml version="1.0"?> <config> <global> <models> <custom_observer> <class>Custom_Observer_Model</class> </custom_observer> </models> </global> </config>
Note here that the models is added within the pair of global
nodes.
As the event name suggests, our observer listens out for the event fired when products have been successfully added to the cart. Looking at the specific configuration below.
<custom_observer> <type>model</type> <class>custom_observer/observer</class> <method>someObserverMethod</method> </custom_observer>
The naming of the custom_observer
nodes is just a unique alias given to our observer.
You can specify a type
of observer. The different types are model
, object
, singleton
or disabled
.
If you choose type disabled
, the observer is disabled and your code will not run. This can be useful should you wish the disable a Magento observer for whatever reason. If you have looked at the dispatchEvent()
method, you’ll note that if you pass in the type as model
, object
or singleton
then the code is executed and the observer will run.
The value within the class
nodes is formed of two parts; the first part before the “/” is from the same name as the alias given when declaring the custom model, under the pair of models
nodes. The second part after the “/” is the name and the path of your model file found under the Model
directory that will execute the code. Note that if our observer file was located in Model/Directory/Observer.php
, then the value within the method
nodes would be custom_observer/directory_observer
.
The value within the method
nodes is the name of our method found within our class that will contain our code to execute when the event is fired.
The last step to add a Magento observer is to add the model file. As specified in config.xml
, the file should be called Observer.php
, and should be located within app/code/local/Custom/Observer/Model/Observer.php
.
Note that you do not have to call the model Observer.php
, but usually Magento also names its observer files this, so let’s keep the naming standard.
<?php class Custom_Observer_Model_Observer { public function someObserverMethod($observer){ Mage::log("This works", null, "Observer.log", true); } }
Here we are simply logging the string This works
to a Observer.log
file found within the var/log
directory.
When we go to add a product to the cart, we should see this log file populate with the This works
string.
When the log file gets populated and therefore you can confirm that your observer is working correctly, you can replace the logging line with actual code that you would like to execute.
And thus concludes the demonstration on how to add a Magento observer.
Note: This article is based on Magento Community/Open Source version 1.9.