Email the customer when tracking numbers are added to their order
Use this task with services that add tracking numbers to fulfillments after fulfillments are created, to ensure that customers are notified when their tracking number is available.
Try this taskWhen it runs
- Occurs whenever a fulfillment is created
- Occurs whenever a fulfillment is updated
What you choose
- Email subject
- Email body
Documentation
Use this task with services that add tracking numbers to fulfillments after fulfillments are created, to ensure that customers are notified when their tracking number is available.
This task works by monitoring created and updated fulfillments. It scans for and saves the tracking numbers currently available for an order, and whenever that set changes, it sends an email to the customer.
Use "TRACKING_NUMBERS" in your email body to insert a list of tracking numbers for the customer's order. Or, use "{{ fulfillment.tracking_number }}", to insert the single tracking number of the fulfillment that has just been created or updated.
Task code and subscriptions
You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.
Subscriptions
shopify/fulfillments/create
shopify/fulfillments/update
How task subscriptions work
Option types and flags
- Email subject (required)
- Email body (required, multiline)
Task code
{% assign email_subject = options.email_subject__required %}
{% assign email_body = options.email_body__required_multiline %}
{% comment %}
-- get order fulfillment data and any previously recorded tracking numbers
{% endcomment %}
{% capture query %}
query {
order(id: {{ fulfillment.order.admin_graphql_api_id | json }}) {
id
email
metafield(key: "mechanic.known_tracking_numbers") {
jsonValue
}
fulfillments {
id
trackingInfo {
number
url
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"order": {
"id": "gid://shopify/Order/1234567890",
"email": "sample@example.com",
"fulfillments": [
{
"trackingInfo": [
{
"number": "1234567890",
"url": null
}
]
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign order = result.data.order %}
{% assign already_known_tracking_numbers = order.metafield.jsonValue | default: array %}
{% assign currently_known_tracking_numbers = array %}
{% assign tracking_numbers_changed = nil %}
{% comment %}
-- check to see if any of the tracking numbers are new
{% endcomment %}
{% for fulfillment in order.fulfillments %}
{% for tracking in fulfillment.trackingInfo %}
{% if tracking.number != blank %}
{% assign currently_known_tracking_numbers = currently_known_tracking_numbers | push: tracking.number %}
{% unless already_known_tracking_numbers contains tracking.number %}
{% assign tracking_numbers_changed = true %}
{% endunless %}
{% endif %}
{% endfor %}
{% endfor %}
{% comment %}
-- if there are changed tracking numnbers, then update known metafield and send customer email
{% endcomment %}
{% if tracking_numbers_changed %}
{% if order.email == blank %}
{% log "No email on file for this order." %}
{% else %}
{% assign tracking_numbers_string = currently_known_tracking_numbers | join: ", " %}
{% action "email" %}
{
"to": {{ order.email | json }},
"subject": {{ email_subject | json }},
"body": {{ email_body | replace: "TRACKING_NUMBERS", tracking_numbers_string | strip | newline_to_br | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}
{% endif %}
{% action "shopify" %}
mutation {
metafieldsSet(
metafields: [
{
ownerId: {{ order.id | json }}
namespace: "mechanic"
key: "known_tracking_numbers"
type: "json"
value: {{ currently_known_tracking_numbers | json | json }}
}
]
) {
metafields {
namespace
key
type
jsonValue
owner {
... on Order {
id
name
}
}
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% endif %}
Mechanic Liquid reference
Default option values
- Email subject
Shipping update for order {{ fulfillment.order.name | default: "(number)" }}- Email body
Hello customer, Track your package: TRACKING_NUMBERS Thanks, {{ shop.name }}