how to add order tracking link on the order confirmation email

how to add order tracking link on the order confirmation email

August 6, 2026 · Illustrated author avatarBy Stefan Bogdanescu
how to add order tracking link on the order confirmation email

Updating Order Confirmation Emails: Adding a Direct Tracking Link for DHL Shipments

As an e-commerce developer, I often receive requests to enhance customer communication around order fulfillment. One common requirement is adding a direct link to the courier’s tracking page within the order confirmation email sent by Magento.

Let’s walk through how to achieve this for shipments handled by DHL in your Magento store.

Step 1: Understanding the Default Order Confirmation Email

By default, Magento sends an order confirmation email using the sales_email_order_new template. This email includes basic order details but lacks a direct link to track the shipment.

The relevant section of the template looks like this (located in app/locale/en_US/template/email/sales/order_new.html):

<!-- message -->
<p{{if $isFronted}} style="font: 12px/1.5em Arial, Helvetica, sans-serif;">{{endregion}}
    <b>Your order # is: {{var order.increment_id}}</b>
    <p>Dear {{var customer.name}},</p>
    <p>Thanks for your recent order from {{config path="general/store_information/name"}}.</p>
    <!-- etc. -->

To add a direct link to the DHL tracking page, we need to modify this template and retrieve the tracking number from the order data.

First, ensure your order data includes the DHL tracking number. In your checkout process, when creating or updating the shipment, you should save the tracking number in the shipment object:

$trackingNumber = 'DC00392903';
$shipment->setTrackingNumbers($trackingNumber);

Next, update the order confirmation email template. We’ll use Magento’s template variables and URL helper to build the link dynamically.

Replace the relevant section with this updated code:

<!-- message -->
<p{{if $isFronted}} style="font: 12px/1.5em Arial, Helvetica, sans-serif;">{{endregion}}
    <b>Your order # is: {{var order.increment_id}}</b>
    <p>Dear {{var customer.name}},</p>
    <p>Thanks for your recent order from {{config path="general/store_information/name"}}.</p>
    <!-- etc. -->
    {% if order.getTracks() %}
        <p><strong>Track your shipment:</strong></p>
        <a href="{{url url='dhl-track' _query={trackingId:order.getTracks()[0].getData('track_number')}}}">DHL Tracking Page</a>
    {% endif %}

Here, we’re using a new variable {% if order.getTracks() %} to check if any shipment tracks exist for the current order. If so, it retrieves the first track’s number and passes it as a query parameter in the URL.

Step 3: Creating the DHL Tracking Page

To make the link work, you need to create an observer that listens for the sales_email_order_new event and adds a new page to your store containing the DHL tracking information.

Here’s a basic example of creating this new page using Magento’s URL rewrites and route configuration:

public function addTrackingPage()
{
    $result = $this->_urlInterface->getUrl('dhl-track');
    $this->_scopeConfig->setValue(
        'system/url_rewrites/default/dhl_track',
        'DHL Tracking Page',
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE
    );
}

When a customer clicks the link in the email, Magento will redirect them to this DHL tracking page with their shipment number pre-filled.

Conclusion

Adding a direct link to your courier’s tracking page within order confirmation emails significantly improves the post-purchase experience for customers. By using Magento’s built-in template system and URL helpers, you can easily integrate this feature into your existing checkout process without needing to modify core files or rely on third-party extensions.

If you found this guide helpful, feel free to explore our comprehensive collection of articles on Magento development best practices for more insights into enhancing customer communication and order management in your e-commerce store.

Checkout Themes Configuration Email

Our network