2

I like to add tracking ID link for my shipping email template form. Please advice me to do this process.

2 Answers 2

4

Please override this file:

vendor/magento/module-sales/view/frontend/templates/email/shipment/track.phtml

by creating this path:

/app/design/frontend/Custom/theme/Magento_Sales/templates/email/shipment/track.phtml

Please add below code in foreach loop

<?php
$trackurl = '';
if($_item->getCarrierCode() === 'fedex'){
    $trackurl = 'https://www.fedex.com/apps/fedextrack/?action=track&trackingnumber='.$_item->getNumber();
}elseif ($_item->getCarrierCode() === 'usps') {
    $trackurl = 'https://tools.usps.com/go/TrackConfirmAction_input?qtc_tLabels1='.$_item->getNumber();
}elseif ($_item->getCarrierCode() === 'ups') {
    $trackurl = 'https://wwwapps.ups.com/WebTracking/returnToDetails?tracknum='.$_item->getNumber();
}
?>
<tr>
    <td><?= $block->escapeHtml($_item->getCarrierCode()) ?>:</td>

     <td style="padding:3px 9px"><a href="<?php echo $trackurl ?>"><?php echo $this->escapeHtml($_item->getNumber()) ?></a></td>

</tr>
0

The better option for M2.4 is to replace Tracking Url Handler with a layout, doing something like that:

app/code/Local/Shipping/view/frontend/layout/sales_email_order_shipment_track.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="sales.order.email.shipment.track">
            <arguments>
                <argument name="tracking_url" xsi:type="object">Local\Shipping\Block\DataProviders\Email\Shipment\TrackingUrl</argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

app/code/Local/Shipping/Block/DataProviders/Email/Shipment/TrackingUrl.php

<?php
declare(strict_types=1);

namespace Local\Shipping\Block\DataProviders\Email\Shipment;

use Magento\Sales\Block\DataProviders\Email\Shipment\TrackingUrl as Core;

class TrackingUrl extends Core
{
    /**
     * Get Shipping tracking URL
     *
     * @param \Magento\Sales\Model\Order\Shipment\Track $track
     * @return string
     */
    public function getUrl(\Magento\Sales\Model\Order\Shipment\Track $track): string
    {
        if ($track->getCarrierCode() === 'something') {
            return 'https://some.url' . '?' . http_build_query(['param' => $track->getNumber()]);
        } else {
            return parent::getUrl($track);
        }
    }
}

Your Answer

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

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