magento training 5 -- part 1

Subject:
When a product is added to one’s wish list, send an email to administrator. 
The email template can be configured In BO.
This function can be disabled in BO.

==============================================================================
1. Create the settings in BO. 
We can see the detail in article "magento training 2". We add a new group in system.xml.


  
  
  
  
  1. <groups>  
  2.                 <wishlist_email_group translate="label" module="hellointershop">  
  3.                     <label>Wishlist email to Admin Options</label>  
  4.                     <frontend_type>text</frontend_type>  
  5.                     <sort_order>1100</sort_order>  
  6.                     <show_in_default>1</show_in_default>  
  7.                     <show_in_website>1</show_in_website>  
  8.                     <show_in_store>1</show_in_store>  
  9.                     <expanded>1</expanded> 
  10.                     <fields>                         
  11.                         <intershop_select translate="label">  
  12.                             <label>Send Email: </label>  
  13.                             <comment>Email to Admin when a product was added to wishlist</comment>  
  14.                             <frontend_type>select</frontend_type>  
  15.                             <sort_order>90</sort_order>  
  16.                             <show_in_default>1</show_in_default>  
  17.                             <show_in_website>1</show_in_website>  
  18.                             <show_in_store>1</show_in_store>  
  19.                             <source_model>adminhtml/system_config_source_yesno</source_model>  
  20.                         </intershop_select> 
  21.                     </fields>  
  22.                 </wishlist_email_group>  
  23.             </groups>  

 

2. Add a new Transactional Emails template in BO. 
System->Transactional Emails->Add New Template. In the new template, we will use 2 variable : 
{{var customerName}}, {{var productName}} which defined in controller.

template subject :
{{var customerName}} add a product {{var productName}} to wishlist

template content :

  
  
  
  
  1. <body style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;"> 
  2. <div style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;"> 
  3. <table cellspacing="0" cellpadding="0" border="0" height="100%" width="100%"> 
  4.     <tr> 
  5.         <td align="center" valign="top" style="padding:20px 0 20px 0"> 
  6.             <!-- [ header starts here] --> 
  7.             <table bgcolor="FFFFFF" cellspacing="0" cellpadding="10" border="0" width="650" style="border:1px solid #E0E0E0;"> 
  8.                 <tr> 
  9.                     <td valign="top"> 
  10.                         <a href="{{store url=""}}" style="color:#1E7EC8;"><img src="{{var logo_url}}" alt="{{var logo_alt}}" border="0"/></a> 
  11.                     </td> 
  12.                 </tr> 
  13.                 <!-- [ middle starts here] --> 
  14.                 <tr> 
  15.                     <td valign="top"> 
  16.                         <p style="font-size:12px; line-height:16px; margin:0 0 16px 0;">Hey,<br/>{{var customerName}} add  product {{var productName}} to his wishlist.</p> 
  17.                         <p style="font-size:12px; line-height:16px; margin:0 0 16px 0;">{{var message}}</p>  
  18.                     </td> 
  19.                 </tr>               
  20.             </table> 
  21.         </td> 
  22.     </tr> 
  23. </table> 
  24. </div> 
  25. </body> 
Actually, you can load the template from exist templates, then modify it.
 3. Create the override controller. Create a new file app/code/local/Kingsun/Hellointershop/controllers/Wishlist/IndexController.php
Here, we assume that we create a new module "intershop" under package "Kingsun" like article 
"magento training 2". 

3.1) Include the core file 

  
  
  
  
  1. require_once('Mage/Wishlist/controllers/IndexController.php'); 

3.2) Create the class name 

  
  
  
  
  1. class Kingsun_Hellointershop_Wishlist_IndexController extends Mage_Wishlist_IndexController 

3.3) Create the function to send email. This email will use the template which we defined for 
Transactional Emails in BO

  
  
  
  
  1. public function sendTransactionalEmail($productName = ''
3.4) Override the addAction. We copy the addAction function from 
app/code/core/Mage/Wishlist/controllers/IndexController.php, then insert the function "sendTransactionalEmail".
  
  
  
  
  1. public function addAction() 

The code is as follows:


  
  
  
  
  1. <?php 
  2. require_once('Mage/Wishlist/controllers/IndexController.php'); 
  3. class Kingsun_Hellointershop_Wishlist_IndexController extends Mage_Wishlist_IndexController 
  4. {    
  5.     /** 
  6.      * Adding new item 
  7.      */  
  8.     public function addAction() 
  9.     {    
  10.         $wishlist = $this->_getWishlist(); 
  11.         if (!$wishlist) { 
  12.             return $this->norouteAction(); 
  13.         } 
  14.  
  15.         $session = Mage::getSingleton('customer/session'); 
  16.  
  17.         $productId = (int) $this->getRequest()->getParam('product'); 
  18.         if (!$productId) { 
  19.             $this->_redirect('*/'); 
  20.             return
  21.         } 
  22.  
  23.         $product = Mage::getModel('catalog/product')->load($productId); 
  24.         if (!$product->getId() || !$product->isVisibleInCatalog()) { 
  25.             $session->addError($this->__('Cannot specify product.')); 
  26.             $this->_redirect('*/'); 
  27.             return
  28.         } 
  29.  
  30.         try { 
  31.             $requestParams = $this->getRequest()->getParams(); 
  32.             if ($session->getBeforeWishlistRequest()) { 
  33.                 $requestParams = $session->getBeforeWishlistRequest(); 
  34.                 $session->unsBeforeWishlistRequest(); 
  35.             } 
  36.             $buyRequest = new Varien_Object($requestParams); 
  37.  
  38.             $result = $wishlist->addNewItem($product$buyRequest); 
  39.             if (is_string($result)) { 
  40.                 Mage::throwException($result); 
  41.             } 
  42.             $wishlist->save(); 
  43.  
  44.             Mage::dispatchEvent( 
  45.                 'wishlist_add_product'
  46.                 array
  47.                     'wishlist'  => $wishlist
  48.                     'product'   => $product
  49.                     'item'      => $result 
  50.                 ) 
  51.             ); 
  52.  
  53.             $referer = $session->getBeforeWishlistUrl(); 
  54.             if ($referer) { 
  55.                 $session->setBeforeWishlistUrl(null); 
  56.             } else { 
  57.                 $referer = $this->_getRefererUrl(); 
  58.             } 
  59.  
  60.             /** 
  61.              *  Set referer to avoid referring to the compare popup window 
  62.              */ 
  63.             $session->setAddActionReferer($referer); 
  64.  
  65.             Mage::helper('wishlist')->calculate(); 
  66.  
  67.              
  68.              
  69.             //Send email to administrator after adding the product to wishlist 
  70.             ///////////////////////////////////////////////////////////////////////////////// 
  71.             if(Mage::getStoreConfig('intershop_options/wishlist_email_group/intershop_select')) 
  72.             { 
  73.                 $this->sendTransactionalEmail($product->getName());      
  74.             } 
  75.             /////////////////////////////////////////////////////////////////////////////// 
  76.              
  77.              
  78.              
  79.             $message = $this->__('%1$s has been added to your wishlist. Click <a href="%2$s">here</a> to continue shopping.'$product->getName(), Mage::helper('core')->escapeUrl($referer)); 
  80.             $session->addSuccess($message); 
  81.         } 
  82.         catch (Mage_Core_Exception $e) { 
  83.             $session->addError($this->__('An error occurred while adding item to wishlist: %s'$e->getMessage())); 
  84.         } 
  85.         catch (Exception $e) { 
  86.             $session->addError($this->__('An error occurred while adding item to wishlist.')); 
  87.         } 
  88.  
  89.         $this->_redirect('*'array('wishlist_id' => $wishlist->getId())); 
  90.          
  91.      
  92.     } 
  93.      
  94.      
  95.     public function sendTransactionalEmail($productName = ''
  96.     {    
  97.         /*$collection =  Mage::getResourceSingleton('core/email_template_collection'); 
  98.         foreach($collection as $value) 
  99.         { 
  100.             echo $value->getTemplateId().'<br/>'; 
  101.             echo $value->getTemplateCode().'<br/>'; 
  102.         }        
  103.         */ 
  104.          
  105.         // Transactional Email Template's ID 
  106.         $templateId = 1; 
  107.              
  108.         // Set sender information            
  109.         $senderName = Mage::getStoreConfig('trans_email/ident_support/name'); 
  110.         $senderEmail = Mage::getStoreConfig('trans_email/ident_support/email'); 
  111.          
  112.         //Set the email sender 
  113.         $senderName = "hfhe2000"
  114.         $senderEmail = "[email protected]";       
  115.         $sender = array('name' => $senderName
  116.                     'email' => $senderEmail); 
  117.          
  118.         //By ID 
  119.         $id = 1; 
  120.         $admin_email = Mage::getModel('admin/user')->load($id)->getData('email'); 
  121.         $admin_firstname = Mage::getModel('admin/user')->load($id)->getData('firstname'); 
  122.         $admin_lastname = Mage::getModel('admin/user')->load($id)->getData('lastname'); 
  123.          
  124.         // Set recepient information 
  125.         $recepientEmail = $admin_email
  126.         $recepientName = $admin_firstname . " " . $admin_lastname;;      
  127.          
  128.         // Get Store ID      
  129.         $store = Mage::app()->getStore()->getId();   
  130.         // Set variables that can be used in email template 
  131.         $customer  = Mage::getSingleton('customer/session')->isLoggedIn() ? Mage::getSingleton('customer/session')->getCustomer()->getData() : null; 
  132.         $vars = array('customerName' => $customer['firstname'] . " " . $customer['lastname'], 
  133.                   'productName' => $productName); 
  134.                  
  135.         $translate  = Mage::getSingleton('core/translate'); 
  136.      
  137.         // Send Transactional Email 
  138.         Mage::getModel('core/email_template'
  139.             ->sendTransactional($templateId$sender$recepientEmail$recepientName$vars$store); 
  140.                  
  141.         $translate->setTranslateInline(true);    
  142.     } 

 

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

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