magento training 5 -- part 2

In the previous article magento taining 5 -- part 1, When we come to the 3rd step, we override the 
function addAction from file app/code/core/Mage/Wishlist/controllers/IndexController.php, then insert
the send email code into this function. It is very boring because we have to repeat the original 
source code. Is there any other solution no need to do that?

Yes. We can add a observer to the event "wishlist_add_product".
1. Register the event with its Observer.
In config.xml, we add the code
  
  
  
  
  1. <global>         
  2.         ... 
  3.  
  4.         <events> 
  5.             <wishlist_add_product> 
  6.                 <observers> 
  7.                     <wishlist_add_observer> 
  8.                         <type>singleton</type> 
  9.                         <class>Kingsun_Hellointershop_Model_Observer</class> 
  10.                         <method>sendMail</method> 
  11.                     </wishlist_add_observer> 
  12.                 </observers> 
  13.             </wishlist_add_product> 
  14.         </events> 
  15.     ... 
  16.  
  17. </global> 
  • <events> - This is the element that stores all of the events that are registered.
  • <wishlist_add_product> - This is the "event" that you are listening to. 
  • <observers> - This is the type of event. I don't think there are others. 
  • <wishlist_add_observer> - This is a unique string that defines this configuration. It can be anything, and just needs to be unique. 
  • <type> - I have always used singleton, but other options can be "model" or "object". The "singleton" will create the object as Mage::getSingleton() while both "object" and "model" will use Mage::getModel() when creating the observer object. 
  • <class> - This is the observer class. The name should be PackageName_ModuleName_Model_Observer. Sometimes the name can be like this ModuleName/Observer. It comes out the same result.
  • <method> - This is the function to be called in the observer class.

2. Add a new file app/code/local/Kingsun/Hellointershop/Model/Observer.php
  
  
  
  
  1. <?php 
  2. class Kingsun_Hellointershop_Model_Observer 
  3.     public function __contruct() 
  4.     { 
  5.     } 
  6.          
  7.     public function sendMail($observer
  8.     {        
  9.         //Send email to administrator after adding the product to wishlist 
  10.         ///////////////////////////////////////////////////////////////////////////////// 
  11.         if(Mage::getStoreConfig('intershop_options/wishlist_email_group/intershop_select')) 
  12.         {    
  13.             $product_name = $observer->getEvent()->getProduct()->getData('name');     
  14.             $this->sendTransactionalEmail($product_name);        
  15.         } 
  16.         /////////////////////////////////////////////////////////////////////////////// 
  17.          
  18.     } 
  19.      
  20.     public function sendTransactionalEmail($productName = ''
  21.     {    
  22.         //The function is the same with previous article
  23. ...
  24.     } 
1) Because the $observer variable can be anything and contain anything, we have to make sure 
our method can handle what is being passed in. In this case it can either have "wishlist", 
"product" or "item" passed in, depending on what checkout you go through. 

   How to search? In Zendstudio, we search "wishlist_add_product" under the folder app/code/core, we can see, 
in file app/code/core/Mage/Wishlist/controllers/IndexController.php, line 192-199,
  
  
  
  
  1. Mage::dispatchEvent( 
  2.       'wishlist_add_product'
  3.        array
  4.                'wishlist'  => $wishlist
  5.                'product'   => $product
  6.                'item'      => $result 
  7.              ) 
  8. ); 
2) Is there other event for wishlist, or more than this? There is a list of all the events. 
See this article Magento Events Cheat Sheet (1.7.0.0) 

 

 

本文出自 “Epicor, magento” 博客,转载请与作者联系!

你可能感兴趣的:(Magento,exercise)