Making use of Observers in Magento

August 28, 2019

When developing for Magento, often most peoples first action is to override a particular class or method from the core. They also usually use the same namespaces and copy into the local folder like: local/Mage/Sales/Model/Orders.php for example. While this is handy for a lot of things, often people don't know, forget about, or don't know how to make use of an Observer when that is 9 times out of 10 the better approach.

In this little tutorial I'll teach you how to create an observer, and you'll also be able to figure out for yourself when to use them too. Awesome stuff, let's get started.

What is an Observer anyway?

In a nutshell: An Observer in Magento listens for when specific events fire, and then executes code we have defined to listen to that observer.

What this allows us to do is create more speration of our code. Especially if it's something we don't really want coded directly into other modules or having to override core functionality which could make it very difficult for you to upgrade in the future.

How do I find what events exist in Magento?

Figuring out how to find these events is relatively simple. We first need to know how Magento fires events and that is done like this:

Mage::dispatchEvent('event_name', array('data' => $data));

With that we can simply do a search in all of our files to return all the events. You can run this command on Linux/Mac:

grep -r "dispatchEvent(" /path/to/magento/install/

Alterntively if you're using Sublime Text 2 or a similar IDE where you can search file contents across an entire project search for dispatchEvent and you'll start to see the results pouring in.

This is fantastic, however it's tricky to digest all this information. Luckily for you there is a fantastic little cheat sheet generated by Nick Jones over on his site. He maintains this and is generally up-to-date and is a fantastic reference. His cheatsheet will show you which file, line and what event is called.

Okay, now how do I create an observer?

By now you will have found the event you're looking for. Now is the time to create our observer in order to execute our desired code! Let's get started.

To do this we will be creating our own module. If you're not familiar with creating your own module don't worry, I'll cover the basics but I won't be going into too much detail.

Before we get started, my example below uses the sales_order_place_before event which is called just before the order is saved. We could do numerous things at this stage such as add additional meta data to the order. In my example, we'll simply output to our log.

Step 1) Create main module xml file

We need to create a file called Meteorify_Observerexample.xml in app/etc/modules.

<?xml version="1.0"?>

This file allows Magento to recongise the modules existence and is where we can deactivate it (by changing <active>true</active> to <active>false</active>)

Step 2) Create folder structure for our module

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

mkdir app/code/local/Meteorify/
mkdir app/code/local/Meteorify/Observerexample/
mkdir app/code/local/Meteorify/Observerexample/etc
mkdir app/code/local/Meteorify/Observerexample/Model

Quick explaination of the module folder structure. Meteorify represents the company or group, then Observerexample is our actual module. etc will contain various configuration files which instructs Magento how to read/use our module. Model contains our various Model classes which our module might use (in this case we will have a Observer.php file in the Model folder).

Step 2) Create our module's configuration file

Each module requires a file called config.xml this file lives in app/code/local/Meteorify/Observerexample/etc

<?xml version="1.0"?>

The above code is our basic config for our model. If you stick to similar naming conventions you'll get on by fine here.

The main code I'd like to highlight here is contained between the <events> tags. The first tag in represents our event we decided to use earlier on sales_order_place_before, so within here we are defining what to do when that event is fired. Within the <observers> tag we have the set up for our Observer. The value within <class> represents the class name of our Observer, and then the value in <method> is the method (or function) we will run when that event is fired.

Step 4) Creating our Observer.php

Now we can create our Observer and place our code inside our method we will create. To do this create a file named Observer.php in app/code/local/Meteorify/Observerexample/Model and place the following code:

class Meteorify_Observerexample_Model_Observer {

    public function example($observer) {
        //$observer contains data passed from when the event was triggered.
        //You can use this data to manipulate the order data before it's saved.
        //Uncomment the line below to log what is contained here:

        Mage::log('We just made an Observer!');


Easy as that.

Now all you need to do is clear the cache, make sure log's are enabled in System > Configuration > Advanced > Developer > Logging and then go through the checkout and then check the logs in var/log.

I've put the completed module onto my github profile, so go ahead and check it out: Observer Example Magento

If you have any questions, or any tips and tricks please feel free to post a comment below!