2015-04-05 32 views

回答

1

對不起,我的解決方案有時候不是很清楚。

出於測試目的,我基於系統/電子郵件模板中現有的「新訂單」創建了新模板。

然後創建system.xml文件,我們可以用模板選擇添加新的領域:

<?xml version="1.0"?> 
<config> 
    <sections> 
     <sales_email> 
      <groups> 
       <order> 
        <fields> 
         <template2> 
          <label>222 New Order Confirmation Template</label> 
          <frontend_type>select</frontend_type> 
          <source_model>adminhtml/system_config_source_email_template</source_model> 
          <sort_order>3</sort_order> 
          <show_in_default>1</show_in_default> 
          <show_in_website>1</show_in_website> 
          <show_in_store>1</show_in_store> 
         </template2> 
         <guest_template2 translate="label"> 
          <label>222 New Order Confirmation Template for Guest</label> 
          <frontend_type>select</frontend_type> 
          <source_model>adminhtml/system_config_source_email_template</source_model> 
          <sort_order>4</sort_order> 
          <show_in_default>1</show_in_default> 
          <show_in_website>1</show_in_website> 
          <show_in_store>1</show_in_store> 
         </guest_template2> 
        </fields> 
       </order> 
      </groups> 
     </sales_email> 
    </sections> 
</config> 

正如你可以看到,我們創建了兩個新的領域模板2guest_template2

然後創建你自己的新模型,它擴展了!!!不重新! Mage_Sales_Model_Order

添加有兩個新的常量

class Vendor_Somemodule_Model_Order extends Mage_Sales_Model_Order 
{ 
    const XML_PATH_EMAIL_TEMPLATE    = 'sales_email/order/template2'; 
    const XML_PATH_EMAIL_GUEST_TEMPLATE   = 'sales_email/order/guest_template2'; 

和複製粘貼的方法sendNewOrderEmail()。在這種方法中刪除兩行底部

$this->setEmailSent(true); 
    $this->_getResource()->saveAttribute($this, 'email_sent'); 

而你的結果將是:

public function sendNewOrderEmail() 
{ 
    $storeId = $this->getStore()->getId(); 

    if (!Mage::helper('sales')->canSendNewOrderEmail($storeId)) { 
     return $this; 
    } 

    $emailSentAttributeValue = $this->load($this->getId())->getData('email_sent'); 
    $this->setEmailSent((bool)$emailSentAttributeValue); 
    if ($this->getEmailSent()) { 
     return $this; 
    } 

    // Get the destination email addresses to send copies to 
    $copyTo = $this->_getEmails(self::XML_PATH_EMAIL_COPY_TO); 
    $copyMethod = Mage::getStoreConfig(self::XML_PATH_EMAIL_COPY_METHOD, $storeId); 

    // Start store emulation process 
    $appEmulation = Mage::getSingleton('core/app_emulation'); 
    $initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId); 

    try { 
     // Retrieve specified view block from appropriate design package (depends on emulated store) 
     $paymentBlock = Mage::helper('payment')->getInfoBlock($this->getPayment()) 
      ->setIsSecureMode(true); 
     $paymentBlock->getMethod()->setStore($storeId); 
     $paymentBlockHtml = $paymentBlock->toHtml(); 
    } catch (Exception $exception) { 
     // Stop store emulation process 
     $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo); 
     throw $exception; 
    } 

    // Stop store emulation process 
    $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo); 

    // Retrieve corresponding email template id and customer name 
    if ($this->getCustomerIsGuest()) { 
     $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_GUEST_TEMPLATE, $storeId); 
     $customerName = $this->getBillingAddress()->getName(); 
    } else { 
     $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $storeId); 
     $customerName = $this->getCustomerName(); 
    } 

    $mailer = Mage::getModel('core/email_template_mailer'); 
    $emailInfo = Mage::getModel('core/email_info'); 
    $emailInfo->addTo($this->getCustomerEmail(), $customerName); 
    if ($copyTo && $copyMethod == 'bcc') { 
     // Add bcc to customer email 
     foreach ($copyTo as $email) { 
      $emailInfo->addBcc($email); 
     } 
    } 
    $mailer->addEmailInfo($emailInfo); 

    // Email copies are sent as separated emails if their copy method is 'copy' 
    if ($copyTo && $copyMethod == 'copy') { 
     foreach ($copyTo as $email) { 
      $emailInfo = Mage::getModel('core/email_info'); 
      $emailInfo->addTo($email); 
      $mailer->addEmailInfo($emailInfo); 
     } 
    } 

    // Set all required params and send emails 
    $mailer->setSender(Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $storeId)); 
    $mailer->setStoreId($storeId); 
    $mailer->setTemplateId($templateId); 
    $mailer->setTemplateParams(array(
      'order'  => $this, 
      'billing'  => $this->getBillingAddress(), 
      'payment_html' => $paymentBlockHtml 
     ) 
    ); 
    $mailer->send(); 

    return $this; 
} 

然後你只需要這個方法本身的Magento調用之前調用此方法放置順序後。

放於config.xml中以下內容:

<global> 
    <models> 
     <somemodule> 
      <class>Vendor_Somemodule_Model</class> 
     </somemodule> 
    </models> 
    <events> 
     <checkout_type_onepage_save_order_after> 
      <observers> 
       <send_duplicate_email> 
        <type>singleton</type> 
        <class>somemodule/observer</class> 
        <method>saveDuplicateEmail</method> 
       </send_duplicate_email> 
      </observers> 
     </checkout_type_onepage_save_order_after> 
    </events> 
</global> 

和創造必要的觀察員:

class Vendor_Somemodule_Model_Observer 
{ 
    public function saveDuplicateEmail($observer) 
    { 
     $orderId = $observer->getOrder()->getId(); 
     $order = Mage::getModel('somemodule/order')->load($orderId); 
     $order->sendNewOrderEmail(); 
    } 
} 
相關問題