0

I have played with this even further and found the issue to be even more simple that what was previously presented. Please read all to get the gist of the issue. I wish to mention here that only the inclusion of a simple "href" is even causing the same error. Here is an example of the code causing the same problem described below.

This is the entire custom email/order/shipment/track.phtml file as modified with the added usps link.

<?php $_shipment=$this->getShipment() ?>
<?php $_order=$this->getOrder() ?>
<?php if ($_shipment && $_order && $_shipment->getAllTracks()): ?>
<table cellspacing="0" cellpadding="0" border="0" width="100%" style="border:1px solid #bebcb7; background:#f8f7f5;">
    <thead>
        <tr>
            <th align="left" style="padding:3px 9px; background:rgb(242,248,228); border-bottom:1px solid #bebcb7;"><?php echo $this->__('Shipped By') ?></th>
            <th align="center" style="padding:3px 9px; background:rgb(242,248,228); border-bottom:1px solid #bebcb7;"><?php echo $this->__('Tracking Number') ?></th>
        </tr>
    </thead>
    <tbody>
    <?php $i=0; foreach ($_shipment->getAllTracks() as $_item): $i++ ?>
        <tr <?php echo $i%2?'bgcolor="#eeeded"':'' ?>>
            <td align="left" valign="top" style="padding:3px 9px">United States Postal Service</td>            
            <td align="center" valign="top" style="padding:3px 9px"><a href="https://tools.usps.com/go/TrackConfirmAction_input?origTrackNum=<?php echo $_item->getNumber() ?>"><?php echo $_item->getNumber() ?></a></td>     
        </tr>
    <?php endforeach ?>
    </tbody>
</table>
<?php endif; ?>

This is the original track.phtml file found in base/default.

<?php $_shipment=$this->getShipment() ?>
<?php $_order=$this->getOrder() ?>
<?php if ($_shipment && $_order && $_shipment->getAllTracks()): ?>
<table cellspacing="0" cellpadding="0" border="0" width="100%" style="border:1px solid #bebcb7; background:#f8f7f5;">
    <thead>
        <tr>
            <th align="left" bgcolor="#d9e5ee" style="padding:3px 9px"><?php echo $this->__('Shipped By') ?></th>
            <th align="center" bgcolor="#d9e5ee" style="padding:3px 9px"><?php echo $this->__('Tracking Number') ?></th>
        </tr>
    </thead>
    <tbody>
    <?php $i=0; foreach ($_shipment->getAllTracks() as $_item): $i++ ?>
        <tr <?php echo $i%2?'bgcolor="#eeeded"':'' ?>>
            <td align="left" valign="top" style="padding:3px 9px"><?php echo $_item->getTitle() ?></td>
            <td align="center" valign="top" style="padding:3px 9px"><?php echo $_item->getNumber() ?></td>
        </tr>
    <?php endforeach ?>
    </tbody>
</table>
<?php endif; ?>

ORIGINAL MESSAGE:

We use a transactional email template for shipping notifications sent to customers when their order has shipped. The template contains the code below to insert the track/phtml block to add the tracking number with URL in the message.

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

Inside track.phtml, we added custom php code to wrap the tracking number is a hyperlink. When entering the tracking number manually and selecting the carrier, we have no problem. The t/n arrives as an active hyperlink. The title is, of course, automatically selected when the carrier is chosen from the drop-down box.

<?php $_shipment=$this->getShipment() ?>
<?php $_order=$this->getOrder() ?>
<?php if ($_shipment && $_order && $_shipment->getAllTracks()): ?>
<table cellspacing="0" cellpadding="0" border="0" width="100%" style="border:1px solid #bebcb7; background:#f8f7f5;">
    <thead>
        <tr>
            <th align="left" style="padding:3px 9px; background:rgb(242,248,228); border-bottom:1px solid #bebcb7;"><?php echo $this->__('Shipped By') ?></th>
            <th align="center" style="padding:3px 9px; background:rgb(242,248,228); border-bottom:1px solid #bebcb7;"><?php echo $this->__('Tracking Number') ?></th>
        </tr>
    </thead>
    <tbody>
    <?php $i=0; foreach ($_shipment->getAllTracks() as $_item): $i++ ?>
        <tr <?php echo $i%2?'bgcolor="#eeeded"':'' ?>>
            <td align="left" valign="top" style="padding:3px 9px"><?php echo $_item->getTitle() ?></td>
                <?php if ($_item->getCarrierCode()=='usps'): ?>
            <td align="center" valign="top" style="padding:3px 9px"><a href="https://tools.usps.com/go/TrackConfirmAction_input?origTrackNum=<?php echo $_item->getNumber() ?>"><?php echo $_item->getNumber() ?></a></td>
                <?php else: ?>
                <?php if ($_item->getCarrierCode()=='fedex'): ?>
            <td align="center" valign="top" style="padding:3px 9px"><a href="http://www.fedex.com/fedextrack/index.html?tracknumbers=<?php echo $_item->getNumber() ?>"><?php echo $_item->getNumber() ?></a></td>
                <?php else: ?>
                <?php if ($_item->getCarrierCode()=='ups'): ?>
            <td align="center" valign="top" style="padding:3px 9px"><a href="http://wwwapps.ups.com/WebTracking/track?HTMLVersion=5.0&loc=en_US&trackNums=<?php echo $_item->getNumber() ?>&track.x=Track"><?php echo $_item->getNumber() ?></a></td>
                <?php else: ?>
                <?php if ($_item->getCarrierCode()=='dhl'): ?>
            <td align="center" valign="top" style="padding:3px 9px"><?php echo $_item->getNumber() ?></td>              
                <?php else: ?>
            <td align="center" valign="top" style="padding:3px 9px"><?php echo $_item->getNumber() ?></td>
                <?php endif; ?>
                <?php endif; ?>
                <?php endif; ?>
                <?php endif; ?>     
        </tr>
    <?php endforeach ?>
    </tbody>
</table>
<?php endif; ?>

We started using a third-party shipping service called ShippingEasy. They automatically return the tracking number to Magento through an API. They also change the title, not the carrier, to contain the name ShippingEasy and a unique transaction number.

Here is the problem. For some reason, the track.phtml block is completely ignored when ShippingEasy delivers the t/n through the API. Their system properly selects the carrier as "usps." The transactional email is sent just fine, but with no tracking number and no table. So, we altered the code in track.phtml to getTitle and look for "ShippingEasy." See below.

<?php $_shipment=$this->getShipment() ?>
<?php $_order=$this->getOrder() ?>
<?php if ($_shipment && $_order && $_shipment->getAllTracks()): ?>
<table cellspacing="0" cellpadding="0" border="0" width="100%" style="border:1px solid #bebcb7; background:#f8f7f5;">
    <thead>
        <tr>
            <th align="left" style="padding:3px 9px; background:rgb(242,248,228); border-bottom:1px solid #bebcb7;"><?php echo $this->__('Shipped By') ?></th>
            <th align="center" style="padding:3px 9px; background:rgb(242,248,228); border-bottom:1px solid #bebcb7;"><?php echo $this->__('Tracking Number') ?></th>
        </tr>
    </thead>
    <tbody>
    <?php $i=0; foreach ($_shipment->getAllTracks() as $_item): $i++ ?>
        <tr <?php echo $i%2?'bgcolor="#eeeded"':'' ?>>
            <td align="left" valign="top" style="padding:3px 9px"><?php echo $_item->getTitle() ?></td>
                <?php if (strpos($_item->getTitle(), 'ShippingEasy') !== false): ?>
            <td align="center" valign="top" style="padding:3px 9px"><a href="https://tools.usps.com/go/TrackConfirmAction_input?origTrackNum=<?php echo $_item->getNumber() ?>"><?php echo $_item->getNumber() ?></a></td>
                <?php else: ?>
                <?php if ($_item->getTitle()=='Federal Express'): ?>
            <td align="center" valign="top" style="padding:3px 9px"><a href="http://www.fedex.com/fedextrack/index.html?tracknumbers=<?php echo $_item->getNumber() ?>"><?php echo $_item->getNumber() ?></a></td>
                <?php else: ?>
                <?php if ($_item->getTitle()=='United Parcel Service'): ?>
            <td align="center" valign="top" style="padding:3px 9px"><a href="http://wwwapps.ups.com/WebTracking/track?HTMLVersion=5.0&loc=en_US&trackNums=<?php echo $_item->getNumber() ?>&track.x=Track"><?php echo $_item->getNumber() ?></a></td>
                <?php else: ?>
                <?php if ($_item->getTitle()=='DHL'): ?>
            <td align="center" valign="top" style="padding:3px 9px"><?php echo $_item->getNumber() ?></td>              
                <?php else: ?>
            <td align="center" valign="top" style="padding:3px 9px"><?php echo $_item->getNumber() ?></td>
                <?php endif; ?>
                <?php endif; ?>
                <?php endif; ?>
                <?php endif; ?>     
        </tr>
    <?php endforeach ?>
    </tbody>
</table>
<?php endif; ?>

However, this still resulted in the API transfer of data from ShippingEasy to Magento to completely ignore inserting the block track.phtml in the transactional email.

Here is the odd part. If we revert to the track.phtml in base/default, the block is recognized and the t/n is transmitted. Furthermore, if we use our custom track.phtml file, we can resend the e-mail by manually clicking the button in Shipments in Order View to "Send Tracking Information" again.

One other thing. The reason the code looks for "ShippingEasy" in the title rather than an exact match is because:

  1. getCarrierCode worked manually when looking for "usps," but did not work with ShippingEasy. <?php if ($_item->getCarrierCode()=='usps'): ?>
  2. Each shipment contains the name ShippingEasy with a unique transaction number. So, we cannot look for an exact match. We were hoping that detecting the "ShippingEasy" string in the title would work.

Before you suggest it, ShippingEasy was of no help in fixing this.

To add to it, we have tested this in both 1.4.1.1 and 1.9.2.1, and the results are the same.

So, what would cause the transfer via API to completely ignore the block only when the custom php or merely the HTML href hyperlink is included in the track.phtml file? Any suggestions?

  • Are you able to log one of the ShippingEasy API calls back to Magento and show it here? – kab8609 Jan 9 '16 at 23:26
  • Honestly, I don't know. Where would that log likely reside? – Scot Jan 10 '16 at 0:24
  • I used this on a 1.7 install and it worked fine. Not sure what 1x version you are on right now. github.com/netz98/N98_ApiLogger. Install this extension and capture a response. – kab8609 Jan 10 '16 at 18:54
  • Well, this is really not cool. I installed it and got nothing. Really weird. The bigger problem is that when I log out of admin, it shows a logout box and then immediately logs me back in. I then have to log out again, but it's only good for that one tab. All other tabs remain logged in. We've never had this happen before. Also, I'm not sure what I should find for evidence of the install in core_resource. So, I restored the DB and disengaged the etc/modules file, and it still keeps a persistent login. Magento is such a pain in the ass! – Scot Jan 11 '16 at 16:10
  • This doesn't sound normal at all. Magento does have a learning curve, however everything happens for a reason. Did you do everything on your own or did you have developers develop the site for you? – kab8609 Jan 13 '16 at 2:23
0

Maybe you run into the same issue i had. I got emails, not containing the correct updated informations.

i figured out (Magento 1.7.2) that the Email for tracking is generated even before the tracking information was saved. Normally all works fine in default, but sometimes it happened, that the email sending is generated before the tracking is generated or updated.

What i were doing is to add an additional $this->save() in app/code/core/Mage/Sales/Model/Order/Shipment.php at the function addTrack right before the return statement.

For sure i didn't do this in the core file itself, i've cloned it into app/code/local/Mage/Sales/Model/Order/Shipment.php

My addTrack looks like this now:

public function addTrack(Mage_Sales_Model_Order_Shipment_Track $track)
{
    $track->setShipment($this)
        ->setParentId($this->getId())
        ->setOrderId($this->getOrderId())
        ->setStoreId($this->getStoreId());
    if (!$track->getId()) {
        $this->getTracksCollection()->addItem($track);
    }

    /**
     * Track saving is implemented in _afterSave()
     * This enforces Mage_Core_Model_Abstract::save() not to skip _afterSave()
     */
    $this->_hasDataChanges = true;
    /**
     * Added additional $this->save(); to have the new data for shippment
     * tracking already in the Mage_Sales_Model_Order_Shipment object.
     * If this would not happen, the tracking mails generated may have old
     * or wrong data regarding carrier title.
     */
    $this->save();

    return $this;
}

EDIT 3nd May 2016: Hi Scot,

in app/code/local/Mage/Sales/Model/Order/Shipment/Api/V2.php and app/code/local/Mage/Sales/Model/Order/Shipment/Api.php i've REMOVED $shipment->sendEmail($email, ($includeComment ? $comment : '')); in the create() function at the try/catch block right after the ->save();

public function create($orderIncrementId, $itemsQty = array(), $comment = null, $email = false,
    $includeComment = false
) {
    $order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);

    /**
      * Check order existing
      */
    if (!$order->getId()) {
         $this->_fault('order_not_exists');
    }

    /**
     * Check shipment create availability
     */
    if (!$order->canShip()) {
         $this->_fault('data_invalid', Mage::helper('sales')->__('Cannot do shipment for order.'));
    }

     /* @var $shipment Mage_Sales_Model_Order_Shipment */
    $shipment = $order->prepareShipment($itemsQty);
    if ($shipment) {
        $shipment->register();
        $shipment->addComment($comment, $email && $includeComment);
        if ($email) {
            $shipment->setEmailSent(true);
        }
        $shipment->getOrder()->setIsInProcess(true);
        try {
            $transactionSave = Mage::getModel('core/resource_transaction')
                ->addObject($shipment)
                ->addObject($shipment->getOrder())
                ->save();
        } catch (Mage_Core_Exception $e) {
            $this->_fault('data_invalid', $e->getMessage());
        }
        return $shipment->getIncrementId();
    }
    return null;
}

But i've added the email send in addTrack()

public function addTrack($shipmentIncrementId, $carrier, $title, $trackNumber)
{
    $shipment = Mage::getModel('sales/order_shipment')->loadByIncrementId($shipmentIncrementId);

    /* @var $shipment Mage_Sales_Model_Order_Shipment */

    if (!$shipment->getId()) {
        $this->_fault('not_exists');
    }

    $carriers = $this->_getCarriers($shipment);

    if (!isset($carriers[$carrier])) {
        $this->_fault('data_invalid', Mage::helper('sales')->__('Invalid carrier specified.'));
    }

    $track = Mage::getModel('sales/order_shipment_track')
                ->setNumber($trackNumber)
                ->setCarrierCode($carrier)
                ->setTitle($title);

    $shipment->addTrack($track);

    try {
        $shipment->save();
        $track->save();
        if(!$shipment->getEmailSent()) {
            $shipment->sendEmail(true, $shipment->getCommentsCollection());
            $shipment->setEmailSent(true);
            $shipment->save();
        }
    } catch (Mage_Core_Exception $e) {
        $this->_fault('data_invalid', $e->getMessage());
    }

    return $track->getId();
}

Hope i didn't mix up something again.

  • Michael, Thank you for responding. For some reason I did not get an email that you responded. I normally do. I tried your suggestion and nothing changed. Any other suggestions you may have are greatly welcomed. – Scot May 2 '16 at 5:34
  • Hi Scot, see my edit please, as it doesnt fit into the comment. – Michael May 2 '16 at 7:19
  • Thank you for the quick response. Did you keep your save() added to app/code/local/Mage/Sales/Model/Order/Shipment.php in addition to these two changes? – Scot May 2 '16 at 18:43
  • yes, because under some circumstances i can't remember now i needed it... – Michael May 2 '16 at 19:30
  • Michael, I'm confused. Both of your edited boxes are the same and I don't see the added $shipment nor sendEmail(). The code you added is the same that I have in both Api.php and V2.php in 1.9.2.2. – Scot May 2 '16 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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