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
Here you can format your comment text
You can then reference this is your custom shipment_new.html template
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.