如何使用不同的模板发送两个magento电子邮件?

时间:2015-04-05 19:53:47

标签: magento

是否可以使用不同的模板在magento上发送两封电子邮件,请说明如何或任何可用的扩展程序?

1 个答案:

答案 0 :(得分: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>

如您所见,我们创建了两个新字段 template2 guest_template2

然后创建自己的新模型,延伸!!! NOT REWRITES !!! 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();
    }
}