Question

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?

Was it helpful?

Solution

This cannot be done only by using a variable name like "order.increment_id" in the email template. You have to send the tracking data to the email template processor to achieve this with an example in 4 steps.

Step1>> Add Module configuration In 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>> Add 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>> Override 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>> Modify your shipment email tempate subject by adding {{var tracks.track_number}}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top