Developer’s Toolbox: Using Magento Events

In this high-level post, I aim to outline what Magento events are, and the benefits to using them. By thinking about events as you define your business logic, you make it easier for you or your developers to get the most benefit out of extensions.

What are Events?

Merriam-Webster defines an event as “something that happens”. It’s a succinct definition, but one that perfectly defines Magento’s event system. Whether it’s a customer registering an account with your store, placing an item in their cart, completing an order, or doing many other specific actions, Magento fires an ‘event’ to allow other sections of code to execute that may be dependent on those activities.

If you’re familiar with JavaScript and the popular jQuery framework, you’ll know that events are fired during actions – mouse click, mouse move, keyboard press, etc. You can then set event handlers to observe that event taking place and run whatever code you need in response to that action. In this way, Magento operates very similarly.

Why use Events?

For a developer, it may be tempting to simply override a Magento class to ensure your code runs. For instance, a number of extensions override the Checkout Success block in order to finalize accounting of some feature it provides. This introduces code complexity, and can cause issues with other extensions that also attempt to override the same block.

By leveraging the event system, you can use an event handler to observe the event. When this event fires, your observer method will receive the message, often with some data attached to it, and execute your code.

In this way, you don’t need to worry about the implications of overriding core blocks and methods. By watching the right handler, you can be sure that your code will have all the information it needs.

Defining Event Observers

An event observer is defined in your extension’s config.xml. In our example extension, simply named ‘Company_Example’, we will implement an observer to the event that Magento fires when the customer views the ‘success’ page after a completed order.

[crayon lang="xml"]






singleton
example/observer
myTestMethod






[/crayon]

In this high-level post, I only want to point out the important parts of our event handlers.

In our event section, we name the event we’re watching, in this case checkout_onepage_controller_success_action. Inside of that, we assign a unique name to our observer (example_success_observer), and define both the class and method to use. We’ve told our extension that the observer lives in models/Observer.php as ‘myTestMethod’:

[crayon lang="php"]
// filename: Company/Example/Model/Observer.php
class Company_Example_Model_Observer
{
public myTestMethod(Varien_Event_Observer $observer)
{
$orderId = $observer->getEvent()->getOrderIds();
$order = Mage::getModel(‘sales/order’)->load($orderId);

/*
From here, you have the order details that have been processed by Magento.

You could report sales information to a third-party accounting system,
notify an internal pick-ticketing system, or basically do anything that you
want with the knowledge that this order is complete.
*/
}
}
[/crayon]

The application possibilities are nearly endless with such a powerful event system. The challenging part is identifying what events are available, and when they are fired.

By now you may be wondering how we came across the event ‘checkout_onepage_controller_success_action‘.
Two places: search engines, and the Magento code.

There’s no definitive list of events in the Magento documentation, but there are a number of third-party attempts at cataloging them all. Perusing these lists may give you some insight as to what actions to direct your extension to observe.

From there, search the Magento code for the event name, and you get both an idea of when it runs (in our easy example, the ‘success’ method of mage/checkout’s OnepageController), and what information it may pass into the event.

There’s no substitute for experimentation! If you’re a Magento developer that’s just getting started using events, creating a quick-and-dirty extension that simply Mage::log()s a message in response to an event would be an excellent way of getting a handle on where and when your events are defined and executed.

Are you interested in learning more about how we leverage Magento’s features and functionality? Contact us to start the conversation!

Got Comments?