1

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.

2 Answers 2

0

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.

1
  • Thanks for the idea. Right after posting this I found a simpler solution, see my comment. I'll mark the question as answered when I can.
    – Sarah
    Commented Feb 24, 2016 at 11:10
0

I've looked at the core code class Mage_Sales_Model_Order_Shipment and found the sendEmail function has a second parameter:

public function sendEmail($notifyCustomer = true, $comment = '')

So changing my sendEmail line fixed the problem.

$shipment->sendEmail(true,$comment)->setEmailSent(true)->save();

I make sure if there is no comment that $comment is an empty string, to fit the parameter's default.

Will mark answer as closed when I can (in 2 days).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.