Magento: Event Observer with Save before and Save after

Magento has this cool functionality of Event Observer. Magento follows the Observer Design Pattern for its event hooking system. With this Event Observer Hook, additional functionality can be added without modifying the core code.

The full list of Magento Event Observer Hooks is present over here:- Magento Event/Observer Hooks Cheat Sheet

Magento has been programmed to raise events in crucial areas of the flow and handling these events for customizations would keep upgradation a simple task that does not require fiddling around with Magento’s core source code.

Event is something that occurs in a certain place during a particular sequence flow.

Observer is the handler of an event. This listens to any event it is attached to and accordingly handles the event.

More description and example on Event Observer and it’s difference with controller override is given over here:- Customizing Magento using Event-Observer Method

From the link just above, you can find example on how you can create and use event observer in Magento.

In this article, I am going to show you how you can use event observer before and after any events in Magento. An example would be the event ‘catalog_product_save_before‘ which will be raised by Magento before a product is saved. Similarly, there is an event ‘catalog_product_save_after‘ is raised by Magento immediately after a product is saved.

Here, we can add our custom event before and after any event raised by Magento’s core. In this way, we don’t have to use the dispatched events.

This way of adding custom event before and after save is useful when we just need to add or do something before or after any Magento core event. For example, setting some values to some attributes before or after product is saved.

For this, you just need to add add some event observer code in your module’s YourModule/etc/config.xml file and add a new observer YourModule/Model/Observer.php

In the example code below, I have added before and after event hook for customer address and catalog product. Customer address event hooks run before and after saving customer address and catalog product hooks run before and after saving products in admin.

customer_address_save_before = runs just before saving customer address
customer_address_save_after = runs immediately after saving customer address

customer_save_before = runs just before saving customer account information
customer_save_after = runs immediately after saving customer account information

catalog_product_save_before = runs just before saving product
catalog_product_save_after = runs just before saving product

Here’s the code:-

config.xml


<global>
    <events>                
        <customer_address_save_before>
            <observers>
                <yourmodule>
                    <type>singleton</type>
                    <class>yourmodule/observer</class>
                    <method>customer_address_save_before</method>
                </yourmodule>
            </observers>
        </customer_address_save_before>
        
        <customer_address_save_after>
            <observers>
                <yourmodule>
                    <type>singleton</type>
                    <class>yourmodule/observer</class>
                    <method>customer_address_save_after</method>
                </yourmodule>
            </observers>
        </customer_address_save_after>

        <customer_save_before>
            <observers>
                <some_unique_identifier>
                    <type>singleton</type>
                    <class>YourNamespace_YourModule_Model_Observer</class>
                    <method>customer_save_before</method>
                </some_unique_identifier>
            </observers>
        </customer_save_before>
        
        <customer_save_after>
            <observers>
                <some_unique_identifier>
                    <type>singleton</type>
                    <class>YourNamespace_YourModule_Model_Observer</class>
                    <method>customer_save_after</method>
                </some_unique_identifier>
            </observers>
        </customer_save_after>
        
        <catalog_product_save_before>
            <observers>
                <yourmodule>
                    <type>singleton</type>
                    <class>yourmodule/observer</class>
                    <method>catalog_product_save_before</method>
                </yourmodule>
            </observers>
        </catalog_product_save_before>
        
        <catalog_product_save_after>
            <observers>
                <yourmodule>
                    <type>singleton</type>
                    <class>yourmodule/observer</class>
                    <method>catalog_product_save_after</method>
                </yourmodule>
            </observers>
        </catalog_product_save_after>       
    </events>
</global>

NOTE: We have written the event observer code inside global node. This will run the event observer code for both frontend and admin. If you need to run the event observer only in the frontend then you need to write the event observer code inside frontend node. Similarly, if you need to run the event observer only in the admin then you need to write the event observer code inside adminhtml node.

Observer.php

<

pre>
class YourNamespace_YourModule_Model_Observer
{<br />
public function customer_address_save_before($observer)
{
$address = $observer->getCustomerAddress();
//echo "

<

pre>"; print_r($address->getData()); exit;
// do something here
}

<pre><code>public function customer_address_save_after($observer)
{
$address = $observer->getCustomerAddress();
//echo "<pre>"; print_r($address->getData()); exit;
// do something here
}

public function customer_save_before($observer)
{
$customer = $observer->getCustomer();
//echo "<pre>"; print_r($customer->getData()); exit;
// do something here
}

public function customer_save_after($observer)
{
$customer = $observer->getCustomer();
//echo "<pre>"; print_r($customer->getData()); exit;
// do something here
}

public function catalog_product_save_before($observer)
{
$product = $observer->getProduct();
//echo "<pre>"; print_r($product->getData()); exit;
// do something here
}

public function catalog_product_save_after($observer)
{
$product = $observer->getProduct();
//echo "<pre>"; print_r($product->getData()); exit;
// do something here
}

}

Similarly, you can add your custom function to other events like sales_order_save_before, sales_order_save_after, sales_order_invoice_save_before, sales_order_invoice_save_after, etc.

Hope this helps. Thanks.