Home » Magento22 February 2011

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

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>
		
		<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>

Observer.php

class YourNamespace_YourModule_Model_Observer
{	
	public function customer_address_save_before($observer)
	{
		$address = $observer->getCustomerAddress();
		//echo "<pre>"; print_r($address->getData()); exit;
		// do something here
	}
	
	public function customer_address_save_after($observer)
	{
		$address = $observer->getCustomerAddress();		
		//echo "<pre>"; print_r($address->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.

From Mukesh Chapagain's Blog, post Magento: Event Observer with Save before and Save after

php magento mukesh chapagain

Get New Post by Email

Find me on

Facebook Twitter Google+ LinkedIn RSS Feed
  • http://www.gersonjanhuel.com/ Gerson Janhuel

    Thanks a lot! :)

  • http://www.facebook.com/vishalsanwar Vishal Sanwar

    Hi,

    After add to cart product in shopping cart which observer will be call?

    Thanks
    Vishal

  • Shazad Khan

    Hello,

    I have a problem.
    I want to change the group id of my customer when he change his country of shipping in his address. I have developped the module correctly with “customer_address_save_after” event.

    Then I change the CUSTOMER OBJET calling a setData(“group_id”, 4) in the address observer.

    In the debug, everything is working fine, i can see the change process before and after the setdata, but when the process finish, the group_id is the same as before….
    Is this because i’m not working on the customer but on the address ?
    How can I fix the problem and correctly save the customer changes ?

  • sunelsaminathan T

    catalog_product_save_before is not working