Magento 聯絡我們功能啟用多個收件人地址
在目前的Magento的系統中,只允許一個收件人接受聯絡表單的訊息,設定聯絡表單的收件人的位置是在 System->Configuration->Contacts 的 “Send Emails To” 欄位
如果我們設定多個收件人,填入數個電子信箱並使用逗號分隔,這會讓我們在後台得到錯誤訊息
解決辦法
如果要啟用聯絡表單多建人這項功能,我們必須對你的Magento做一些程式碼的改寫,以下為本次範例.
第一,我們需要建立下列這些資料夾結構
mkdir –p app/code/local/Astralweb/Contacts/
mkdir –p app/code/local/Astralweb/Contacts/controllers/
mkdir app/code/local/Astralweb/Contacts/etc/
建立檔案 app/etc/modules/Astralweb_Contacts.xml .
<?xml version="1.0"?> <!-- /** * Astralweb * * Contacts - allow Multiemail */ --> <config> <modules> <Astralweb_Contacts> <active>true</active> <codePool>local</codePool> </Astralweb_Contacts> </modules> </config>
現在,我們要建立檔案覆寫 Magento 核心的 Controller 檔案名稱為app/code/local/Astralweb/Contacts/controllers/IndexController.php
我們需要 extend Mage_Contacts_IndexController 並且覆寫 postAction() 這個功能,postAction這個功能是用來處理將訊息寄送給管理者
並且你需要在檔案中先載入原先的 Controller 檔案
require_once ‘Mage/Contacts/controllers/IndexController.php’;
建立類別並 extend 聯絡表單的 IndexController class:
class Astralweb_Contacts_IndexController extends Mage_Contacts_IndexController {
}
下一步,我們把原生 postAction 的程式碼從原生檔案(“app/code/core/Mage/Contacts/controllers/IndexController.php“) 複製到我們新建的Controller,接著我們要改寫這個功能
public function postAction() { $post = $this->getRequest()->getPost(); if ( $post ) { $translate = Mage::getSingleton('core/translate'); /* @var $translate Mage_Core_Model_Translate */ $translate->setTranslateInline(false); try { $postObject = new Varien_Object(); $postObject->setData($post); $error = false; if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) { $error = true; } if (!Zend_Validate::is(trim($post['comment']) , 'NotEmpty')) { $error = true; } if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) { $error = true; } if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) { $error = true; } if ($error) { throw new Exception(); } $mailTemplate = Mage::getModel('core/email_template'); /* @var $mailTemplate Mage_Core_Model_Email_Template */ $mailTemplate->setDesignConfig(array('area' => 'frontend')) ->setReplyTo($post['email']) ->sendTransactional( Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE), Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER), Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT), null, array('data' => $postObject) ); if (!$mailTemplate->getSentSuccess()) { throw new Exception(); } $translate->setTranslateInline(true); Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.')); $this->_redirect('*/*/'); return; } catch (Exception $e) { $translate->setTranslateInline(true); Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later')); $this->_redirect('*/*/'); return; } } else { $this->_redirect('*/*/'); } }
加入下列程式碼:
$email_string = Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT); $emailArray = explode(',', $email_string);
在下列程式碼之前:
$mailTemplate = Mage::getModel('core/email_template');
這將會把我們在後台所建立的字串,透過PHP function 依逗號將字串切割成陣列,所以在後台設立多收件人十,分隔符號你可以選你喜愛的符號,但是在這邊就要相對地修改
現在我們將我們的代碼加入在這行代碼之後
$mailTemplate = Mage::getModel('core/email_template');
並且在這段代碼結束之前:
if (!$mailTemplate->getSentSuccess()) { throw new Exception(); }
把陣列放在 foreach 的 loop 中
foreach ($emailArray as $email)
程式碼應該會看起來像下列的代碼
$email_string = Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT); $emailArray = explode(',', $email_string); foreach ($emailArray as $email) { $mailTemplate = Mage::getModel('core/email_template'); /* @var $mailTemplate Mage_Core_Model_Email_Template */ $mailTemplate->setDesignConfig(array('area' => 'frontend')) ->setReplyTo($post['email']) ->sendTransactional( Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE), Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER), Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT), null, array('data' => $postObject) ); if (!$mailTemplate->getSentSuccess()) { throw new Exception(); } }
現在將原本的程式碼
Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
取代為在 Foreach loop 中的值:
trim($emaiL),
程式碼應該會看起來像下列的代碼
<?php require_once 'Mage/Contacts/controllers/IndexController.php'; class Astralweb_Contacts_IndexController extends Mage_Contacts_IndexController { public function postAction() { $post = $this->getRequest()->getPost(); if ($post) { $translate = Mage::getSingleton('core/translate'); /* @var $translate Mage_Core_Model_Translate */ $translate->setTranslateInline(false); try { $postObject = new Varien_Object(); $postObject->setData($post); $error = false; if (!Zend_Validate::is(trim($post['name']), 'NotEmpty')) { $error = true; } if (!Zend_Validate::is(trim($post['comment']), 'NotEmpty')) { $error = true; } if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) { $error = true; } if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) { $error = true; } if ($error) { throw new Exception(); } $email_string = Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT); $emailArray = explode(',', $email_string); foreach ($emailArray as $email) { $mailTemplate = Mage::getModel('core/email_template'); /* @var $mailTemplate Mage_Core_Model_Email_Template */ $mailTemplate->setDesignConfig(array('area' => 'frontend')) ->setReplyTo($post['email']) ->sendTransactional( Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE), Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER), trim($email), null, array('data' => $postObject) ); if (!$mailTemplate->getSentSuccess()) { throw new Exception(); } } $translate->setTranslateInline(true); Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.')); $this->_redirect('*/*/'); return; } catch (Exception $e) { $translate->setTranslateInline(true); Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later')); $this->_redirect('*/*/'); return; } } else { $this->_redirect('*/*/'); } } }
我們加入了Foreach迴圈,所以我們可以把Email發送至所有我們指定的收件人
下一個需要被建立的檔案是
app/code/local/Astralweb/Contacts/etc/config.xml
<?xml version="1.0"?> <config> <modules> <Astralweb_Contacts> <version>0.1.0</version> </Astralweb_Contacts> </modules> <frontend> <routers> <contacts> <args> <modules> <Astralweb_Contacts before="Mage_Contacts">Astralweb_Contacts</Astralweb_Contacts> </modules> </args> </contacts> </routers> </frontend> </config>
與
app/code/local/Astralweb/Contacts/etc/system.xml
<?xml version="1.0"?> <config> <sections> <contacts><!-- first part of the path --> <groups> <email> <fields> <recipient_email> <validate>required-entry</validate> </recipient_email> </fields> </email> </groups> </contacts> </sections> </config>
我們覆寫了聯絡表單的原生 system.xml,這會將後台的欄位驗證錯誤給消除
在清除快取之後,去 System->Configuration->Contacts 的 “Send Emails To” 欄位,輸入多個收件人地址,並使用逗號分隔電子信箱,接著儲存.
現在客戶使用聯絡表單功能時,就會將聯絡訊息寄送給多個收件人
確認送出聯絡訊息後會得到這樣的訊息
我要留言