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.
- <groups>
- <wishlist_email_group translate="label" module="hellointershop">
- <label>Wishlist email to Admin Options</label>
- <frontend_type>text</frontend_type>
- <sort_order>1100</sort_order>
- <show_in_default>1</show_in_default>
- <show_in_website>1</show_in_website>
- <show_in_store>1</show_in_store>
- <expanded>1</expanded>
- <fields>
- <intershop_select translate="label">
- <label>Send Email: </label>
- <comment>Email to Admin when a product was added to wishlist</comment>
- <frontend_type>select</frontend_type>
- <sort_order>90</sort_order>
- <show_in_default>1</show_in_default>
- <show_in_website>1</show_in_website>
- <show_in_store>1</show_in_store>
- <source_model>adminhtml/system_config_source_yesno</source_model>
- </intershop_select>
- </fields>
- </wishlist_email_group>
- </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 :
- <body style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
- <div style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
- <table cellspacing="0" cellpadding="0" border="0" height="100%" width="100%">
- <tr>
- <td align="center" valign="top" style="padding:20px 0 20px 0">
- <!-- [ header starts here] -->
- <table bgcolor="FFFFFF" cellspacing="0" cellpadding="10" border="0" width="650" style="border:1px solid #E0E0E0;">
- <tr>
- <td valign="top">
- <a href="{{store url=""}}" style="color:#1E7EC8;"><img src="{{var logo_url}}" alt="{{var logo_alt}}" border="0"/></a>
- </td>
- </tr>
- <!-- [ middle starts here] -->
- <tr>
- <td valign="top">
- <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>
- <p style="font-size:12px; line-height:16px; margin:0 0 16px 0;">{{var message}}</p>
- </td>
- </tr>
- </table>
- </td>
- </tr>
- </table>
- </div>
- </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
- require_once('Mage/Wishlist/controllers/IndexController.php');
3.2) Create the class name
- 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
- 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".
- public function addAction()
The code is as follows:
- <?php
- require_once('Mage/Wishlist/controllers/IndexController.php');
- class Kingsun_Hellointershop_Wishlist_IndexController extends Mage_Wishlist_IndexController
- {
- /**
- * Adding new item
- */
- public function addAction()
- {
- $wishlist = $this->_getWishlist();
- if (!$wishlist) {
- return $this->norouteAction();
- }
- $session = Mage::getSingleton('customer/session');
- $productId = (int) $this->getRequest()->getParam('product');
- if (!$productId) {
- $this->_redirect('*/');
- return;
- }
- $product = Mage::getModel('catalog/product')->load($productId);
- if (!$product->getId() || !$product->isVisibleInCatalog()) {
- $session->addError($this->__('Cannot specify product.'));
- $this->_redirect('*/');
- return;
- }
- try {
- $requestParams = $this->getRequest()->getParams();
- if ($session->getBeforeWishlistRequest()) {
- $requestParams = $session->getBeforeWishlistRequest();
- $session->unsBeforeWishlistRequest();
- }
- $buyRequest = new Varien_Object($requestParams);
- $result = $wishlist->addNewItem($product, $buyRequest);
- if (is_string($result)) {
- Mage::throwException($result);
- }
- $wishlist->save();
- Mage::dispatchEvent(
- 'wishlist_add_product',
- array(
- 'wishlist' => $wishlist,
- 'product' => $product,
- 'item' => $result
- )
- );
- $referer = $session->getBeforeWishlistUrl();
- if ($referer) {
- $session->setBeforeWishlistUrl(null);
- } else {
- $referer = $this->_getRefererUrl();
- }
- /**
- * Set referer to avoid referring to the compare popup window
- */
- $session->setAddActionReferer($referer);
- Mage::helper('wishlist')->calculate();
- //Send email to administrator after adding the product to wishlist
- /////////////////////////////////////////////////////////////////////////////////
- if(Mage::getStoreConfig('intershop_options/wishlist_email_group/intershop_select'))
- {
- $this->sendTransactionalEmail($product->getName());
- }
- ///////////////////////////////////////////////////////////////////////////////
- $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));
- $session->addSuccess($message);
- }
- catch (Mage_Core_Exception $e) {
- $session->addError($this->__('An error occurred while adding item to wishlist: %s', $e->getMessage()));
- }
- catch (Exception $e) {
- $session->addError($this->__('An error occurred while adding item to wishlist.'));
- }
- $this->_redirect('*', array('wishlist_id' => $wishlist->getId()));
- }
- public function sendTransactionalEmail($productName = '')
- {
- /*$collection = Mage::getResourceSingleton('core/email_template_collection');
- foreach($collection as $value)
- {
- echo $value->getTemplateId().'<br/>';
- echo $value->getTemplateCode().'<br/>';
- }
- */
- // Transactional Email Template's ID
- $templateId = 1;
- // Set sender information
- $senderName = Mage::getStoreConfig('trans_email/ident_support/name');
- $senderEmail = Mage::getStoreConfig('trans_email/ident_support/email');
- //Set the email sender
- $senderName = "hfhe2000";
- $senderEmail = "[email protected]";
- $sender = array('name' => $senderName,
- 'email' => $senderEmail);
- //By ID
- $id = 1;
- $admin_email = Mage::getModel('admin/user')->load($id)->getData('email');
- $admin_firstname = Mage::getModel('admin/user')->load($id)->getData('firstname');
- $admin_lastname = Mage::getModel('admin/user')->load($id)->getData('lastname');
- // Set recepient information
- $recepientEmail = $admin_email;
- $recepientName = $admin_firstname . " " . $admin_lastname;;
- // Get Store ID
- $store = Mage::app()->getStore()->getId();
- // Set variables that can be used in email template
- $customer = Mage::getSingleton('customer/session')->isLoggedIn() ? Mage::getSingleton('customer/session')->getCustomer()->getData() : null;
- $vars = array('customerName' => $customer['firstname'] . " " . $customer['lastname'],
- 'productName' => $productName);
- $translate = Mage::getSingleton('core/translate');
- // Send Transactional Email
- Mage::getModel('core/email_template')
- ->sendTransactional($templateId, $sender, $recepientEmail, $recepientName, $vars, $store);
- $translate->setTranslateInline(true);
- }
- }
本文出自 “Epicor, magento” 博客,转载请与作者联系!