Magento Event Observer with Save before and Save after


/ Published in: PHP
Save to your folder(s)

http://codemagento.com/2011/04/observers-and-dispatching-events/


Copy this code and paste it in your HTML
  1. config.xml
  2. <global>
  3. <events>
  4. <customer_address_save_before>
  5. <observers>
  6. <yourmodule>
  7. <type>singleton</type>
  8. <class>yourmodule/observer</class>
  9. <method>customer_address_save_before</method>
  10. </yourmodule>
  11. </observers>
  12. </customer_address_save_before>
  13.  
  14. <customer_address_save_after>
  15. <observers>
  16. <yourmodule>
  17. <type>singleton</type>
  18. <class>yourmodule/observer</class>
  19. <method>customer_address_save_after</method>
  20. </yourmodule>
  21. </observers>
  22. </customer_address_save_after>
  23.  
  24. <catalog_product_save_before>
  25. <observers>
  26. <yourmodule>
  27. <type>singleton</type>
  28. <class>yourmodule/observer</class>
  29. <method>catalog_product_save_before</method>
  30. </yourmodule>
  31. </observers>
  32. </catalog_product_save_before>
  33.  
  34. <catalog_product_save_after>
  35. <observers>
  36. <yourmodule>
  37. <type>singleton</type>
  38. <class>yourmodule/observer</class>
  39. <method>catalog_product_save_after</method>
  40. </yourmodule>
  41. </observers>
  42. </catalog_product_save_after>
  43. </events>
  44. </global>
  45.  
  46. Observer.php
  47. class YourNamespace_YourModule_Model_Observer
  48. {
  49. public function customer_address_save_before($observer)
  50. {
  51. $address = $observer->getCustomerAddress();
  52. //echo "<pre>"; print_r($address->getData()); exit;
  53. // do something here
  54. }
  55.  
  56. public function customer_address_save_after($observer)
  57. {
  58. $address = $observer->getCustomerAddress();
  59. //echo "<pre>"; print_r($address->getData()); exit;
  60. // do something here
  61. }
  62.  
  63. public function catalog_product_save_before($observer)
  64. {
  65. $product = $observer->getProduct();
  66. //echo "<pre>"; print_r($product->getData()); exit;
  67. // do something here
  68. }
  69.  
  70. public function catalog_product_save_after($observer)
  71. {
  72. $product = $observer->getProduct();
  73. //echo "<pre>"; print_r($product->getData()); exit;
  74. // do something here
  75. }
  76. }

URL: http://blog.chapagain.com.np/magento-event-observer-with-save-before-and-save-after/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.