Magento – How to add tracking url in shipment email for customer in Magento 2

email-templatesmagento2shipmentshipment-tracking

When I ship any order with a UPS label and add the tracking number. it shows Track Order in the backend. I want to send this URL to the customer's email also. Currently, it sends the tracking number and shipment method only.

Magento 2 shipment email

Best Answer

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>
Related Topic