Magento – Include Comment in Programmatically Sent Shipment Email

emailorder-emailshipment

I have some code which programmatically sends out a shipment email to a customer. A shipment comment is added using addComment.

$shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($itemsToShip);

if($shipment) {
    try {

        $shipment->register();
        $shipment->getOrder()->setIsInProcess(true);
        // without this line below the shipment is created (above) but the order line is not shown as 'Shipped' on the main order view
        $transactionSave = Mage::getModel('core/resource_transaction')->addObject($shipment)->addObject($shipment->getOrder())->save();

        if (!empty($carrier)) {
            $comment = $this->getShipmentComment($carrier['id'],$carrier['reference']);
            if ($comment) {
                $shipment->addComment($comment, true);
            }
        }

        $shipment->sendEmail(true)->setEmailSent(true)->save(); 
        return true;

    }

    catch (Exception $e) {
        Mage::logException($e);
    }
}

The shipment is created, comment added, and email sent. The only issue is that, while the comment is added and visible in the backend, the comment is not included in the email sent out. I have got {{var comments}} included in my shipment email template.

        {{if comment}}
        <table cellspacing="0" cellpadding="0" class="message-container">
            <tr>
                <td>{{var comment}}</td>
            </tr>
        </table>
        {{/if}}

An example of the comment added is:

Your parcel is being shipped by DPD with reference number XXXXXX. You can track your shipment using this link: <a href='http://www.dpd.co.uk/search.jsp?q=XXXXXX'>http://www.dpd.co.uk/search.jsp?q=XXXXXX</a>

In the backend it says against the Comment 'Customer notified' which I believe relates to the second parameter of the addComment function being true.

I'm thinking there may be a parameter in the sendEmail function which relates to the backend "Append Comments" function. I can't find any documentation for these functions, though, I've put this code together from various sources.

Best Answer

The way I would do this is to add a new template to the shipment templates in

yourtemplate/email/order/shipment/comment.phtml

Here you can format your comment text

<?php $_shipment=$this->getShipment() ?>
<?php $_order=$this->getOrder() ?>
<?php if ($_shipment && $_order && $_shipment->getAllTracks()): ?>
<?php $_trackingNumbers=array(); ?>
<?php $i=0; foreach ($_shipment->getAllTracks() as $_item): $i++ ?>
<?php $_trackingNumbers[]=$this->escapeHtml($_item->getNumber()) ?>
<?php $_shipmentType=$this->escapeHtml($_item->getTitle()) ?>
<?php endforeach ?>

<h2>NOTICE</h2>
<p>Your parcel is being shipped by DPD with reference number <?php echo implode(', ',$_trackingNumbers) ?>.</p>

You can then reference this is your custom shipment_new.html template

{{block type='core/template' area='frontend' template='email/order/shipment/comment.phtml' shipment=$shipment order=$order}}    

Here I am also passing in the shipment and order object so you can also extract order and shipping information from this i.e. tracking number etc.