An Introduction to Magento Events

Posted by Jason Young

March 19, 2014 | 12:28 PM

Share this blog on:     

magento_logo

What are Events?

Events are exactly what they sound like: something that happens. In Magento-specific terms, they are handlers that allow other parts of your code to react to actions that take place in the application. 

For instance, you may want to add a free catalog to the order when someone places a product into his or her cart. By listening for, or observing, that event, you can be sure the appropriate action will be taken.

Why should I use Events?

As a developer, using Events can greatly simplify your code and allows for re-use. By not defining your requirements directly within your controllers or block classes, you reduce a lot of clutter and you can often avoid rewriting core Magento Block classes. This makes for cleaner, more manageable modules. 

Observing an Event

An event observer is defined in your extension's config.xml. Perhaps you have a module named, for example, Acme_AddCatalog, that automatically adds a physical catalog to a customer's cart. We will want to run this when another product is added to the cart. 

Here is an example of how that might look in your module. I will only be calling attention to the implementation of the Event, rather than providing an entire extension. 

[xml]

<config>

    <modules>

        <Acme_AddCatalog>

            <version>0.1.0</version>

        </Acme_AddCatalog>

    </modules>

    <global>

        <models>

            <acme_catalog>

                <class>Acme_AddCatalog_Model</class>

            </acme_catalog>

        </models>

 

        <!-- More definitions here -->

 

        <events>

            <checkout_cart_add_product_complete>

                <observers>

                    <acme_add_catalog>

                        <type>singleton</type>

                        <class>acme_catalog/observer</class>

                        <method>addProductCatalogToCart</method>

                    </acme_add_catalog>

                </observers>

            </checkout_cart_add_product_complete>

        </events>

    </global>

</config>

[/xml]

Pointing out the Event-specific configuration, you can see the following hierarchy:

checkout_cart_add_product_complete specifically defines the event we want to observe. In this case, the event that fires after a product has been fully added to the cart.

observers is the parent container for all of the event handlers you wish to define. You are free to add as many event handlers as you wish within this container.

acme_add_catalog defines a unique name for the event’s Observer. You can use your module name or some other descriptive name to define this. If this is not unique, you run the risk of overriding existing definitions, instead of adding a new one.  Remember that it is per Event, so acme_add_catalog would be a valid identifier for another event in this module.

type identifies the type of class you are loading. Typically you would want to keep this as 'singleton' to maintain a single instance of the Observer class, but you can also use ‘object’ or ‘model’ to load a completely independent instance.

class defines which class to load for the Observer. In this case, we are using Magento’s Model resolver. acme_catalog is our alias (see the global/models tag), and observer will be the name of our Observer class. This could also defined as Acme_Catalog_Model_Observer.

method tells Magento which method in your Observer class to fire. We will see an example of this next.

Now that we have our configuration defined, we will need to creat our Observer class. Generally, and following the definition in our example, this will be Models/Observer.php from your module’s root.

[php]

/* filename: Acme/Catalog/Model/Observer.php */

class Acme_Catalog_Model_Observer

{

    public addProductCatalogToCart(Varien_Event_Observer $observer)

    {

        $catalogProductId = ‘1234’;

        $product = $observer->getEvent()->getProduct();

 

        // If the added product is our Catalog, we don’t need to do anything further

        if ($product->getId() != $catalogProductId) {

            return true;

        }

 

        /*

            From here, you would add the code that checks whether

            your catalog is already in the cart, and add it if not.

        */

    }

}

[/php]

Unfortunately, there is no definitive list of events in any Magento-provided documentation, but by reading the code and utilizing search engines, you can usually find a good place to create your events.

There is no substitute for experimentation! It is certainly an exercise in patience if you are venturing into an event type you have not used before, but the rewards can be well worth the effort. 

magento-platform-checklist

Topics: Magento, Magento Technical

About Jason Young

Jason Young is a Senior Magento Developer. Having worked as a Magento developer for some time, Jason enjoys the problem-solving aspect of adding new functionality to Magento sites in order to fit the needs of clients. As a personal achievement, Jason created and moderated the largest Xbox community website for six years, seeing over 5 million users and 12 million hits a day worldwide.

Search

Subscribe to Email Updates

magento-platform-checklist
New Call-to-Action

Contact Us

B2B-2.0

Recent Posts