0

As we send shippment emails to customers, in this shippment email, we get tracking number from track.phtml and now the subject part of the email is like, order number and shippment number. I would like to change the subject of the email in which we are senidng as shippment emails. The subject has to be with tracking number. For order number we can get with "order.increment_id", But I dont know how to get the tracking number. SO how can I display tracking number in subject of email?

4

1 回答 1

3

这不能仅通过在电子邮件模板中使用变量名称(如“order.increment_id”)来完成。您必须通过 4 个步骤的示例将跟踪数据发送到电子邮件模板处理器以实现此目的。

Step1>> 在 app/etc/modules/Eglobe_Sales.xml添加模块配置

<?xml version="1.0"?>
<config>
    <modules>
        <Eglobe_Sales>
            <active>true</active>
            <codePool>local</codePool>
        </Eglobe_Sales>
    </modules>
</config>

Step2>> 添加config.xml(app/code/local/Eglobe/Sales/etc/config.xml)

<?xml version="1.0"?>
<config>
    <modules>
        <Eglobe_Sales>
            <version>0.1.0</version>
        </Eglobe_Sales>
    </modules>
    <global>
        <models>
            <sales>
                <rewrite>
            <order_shipment>Eglobe_Sales_Model_Order_Shipment</order_shipment>
        </rewrite>
            </sales>
        </models>
    </global>
</config>

Step3>> 覆盖 Mage_Sales_Model_Order_Shipment::sendEmail()

     <?php

class Eglobe_Sales_Model_Order_Shipment extends Mage_Sales_Model_Order_Shipment {

    /**
     * Send email with shipment data
     *
     * @param boolean $notifyCustomer
     * @param string $comment
     * @return Mage_Sales_Model_Order_Shipment
     */
    public function sendEmail($notifyCustomer = true, $comment = '')
    {
        $order = $this->getOrder();
        $storeId = $order->getStore()->getId();

        if (!Mage::helper('sales')->canSendNewShipmentEmail($storeId)) {
            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);
        // Check if at least one recepient is found
        if (!$notifyCustomer && !$copyTo) {
            return $this;
        }

        // 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($order->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 ($order->getCustomerIsGuest()) {
            $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_GUEST_TEMPLATE, $storeId);
            $customerName = $order->getBillingAddress()->getName();
        } else {
            $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $storeId);
            $customerName = $order->getCustomerName();
        }

        $mailer = Mage::getModel('core/email_template_mailer');
        if ($notifyCustomer) {
            $emailInfo = Mage::getModel('core/email_info');
            $emailInfo->addTo($order->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' or a customer should not be notified
        if ($copyTo && ($copyMethod == 'copy' || !$notifyCustomer)) {
            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' => $order,
            'shipment' => $this,
            'comment' => $comment,
            'billing' => $order->getBillingAddress(),
            'payment_html' => $paymentBlockHtml,
//setting the `track number here, A shihpment can have more than one track numbers so seperating by comma`
            'tracks' => new Varien_Object(array('tracking_number' => implode(',', $this->getTrackingNumbers())))
                )
        );
        $mailer->send();

        return $this;
    }

    //Creating track number array
    public function getTrackingNumbers()
    {
        $tracks = $this->getAllTracks();
        $trackingNumbers = array();
        if (count($tracks)) {
            foreach ($tracks as $track) {
                $trackingNumbers[] = $track->getNumber();
            }
        }
        return $trackingNumbers;
    }

}

Step4>> 通过添加 {{var tracking.track_number}} 修改您的发货电子邮件模板主题

于 2013-09-26T15:32:56.153 回答