I have a client that is trying to get their order tracking numbers displayed to the customer on their order page (/account/orders/...)
I have looked up Shopify's docs but it seems like the fulfillment object is only available in email templates ...
Is there something I am missing? Has anyone been able to put the order tracking number on the order template before?
Did a bit of research and I found out why this was not working for me ...
Shopify offers the ability for merchants to ship part of an order. This means that if someone ordered 5 different products and the merchant could only ship 3 today and 2 next week, there would be two different tracking numbers and two different URLs for tracking.
Because Shopify offers this, I had to use a work-around to get the tracking number and other info on the order page using a for loop. Here's what my code looks like.
<ul class="order-status"> <li>Status: <span>{{ order.fulfillment_status_label }}</span></li> {% if order.fulfillment_status == 'fulfilled' %} <li>Tracking Number: {% for line in order.line_items %} {% if line.fulfillment.tracking_number %} <span>{{ line.fulfillment.tracking_number }}</span> {% endif %} {% endfor %} </li> <li>Tracking Company: {% for line in order.line_items %} {% if line.fulfillment.tracking_company %} <span>{{ line.fulfillment.tracking_company }}</span> {% endif %} {% endfor %} </li> <li>URL: {% for line in order.line_items %} {% if line.fulfillment.tracking_url %} <a href="{{ line.fulfillment.tracking_url }}">Tracking URL</a> {% endif %} {% endfor %} </li> {% endif %} </ul>
There might be a better way to code this, but it is working on my client's theme ATM.
Open to suggestions for improvement. Thanks!
Nice James, this is really helpful. Thank you!
Question for you. What's the best way to only display each tracking number once?
We never split our orders into multiple shipments, so each order only ever has one tracking number. But we also have orders with lots of line items, often 20 or more. In those cases the same tracking number is displayed 20 or times, which isn't great for UX.
This worked for me:
{% for line_item in order.line_items %} {% if forloop.first == true %} <a href="{{ line_item.fulfillment.tracking_url }}" target="_blank">{{ line_item.fulfillment.tracking_number }}</a> {% endif %} {% endfor %}
User | Count |
---|---|
28 | |
18 | |
15 | |
14 | |
13 |