Add fulfillment tracking when an order is tagged
Configure this tag with a mapping of country codes to tracking companies and URL templates, and/or default companies and URL templates. When an order is tagged, this task will apply the appropriate values to each fulfillment that has a tracking number.
Try this taskWhen it runs
Occurs whenever an order is updated
What you choose
- Order tag to watch for
- Country codes and tracking URL templates
- Default tracking URL template
Show 4 more
- Overwrite existing tracking URLs
- Country codes and tracking companies
- Default tracking company
- Notify customers
Documentation
Configure this tag with a mapping of country codes to tracking companies and URL templates, and/or default companies and URL templates. When an order is tagged, this task will apply the appropriate values to each fulfillment that has a tracking number.
Configure this tag with mappings of two-character country codes to tracking companies, and tracking URL templates. Both companies and URL templates support a default value as well, which will be used for orders that don't have a mapped country in the task's configuration. When an order is tagged, this task will apply the appropriate values to each fulfillment that has a tracking number.
If "Notify customers" is enabled, this task will request that Shopify send a notification email when the fulfillment update is processed.
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/orders/updated
How task subscriptions work
Option types and flags
- Order tag to watch for (required)
- Country codes and tracking URL templates (keyval)
- Overwrite existing tracking URLs (boolean)
- Country codes and tracking companies (keyval)
- Notify customers (boolean)
Task code
{% comment %}
Preferred option order:
{{ options.order_tag_to_watch_for__required }}
{{ options.country_codes_and_tracking_url_templates__keyval }}
{{ options.default_tracking_url_template }}
{{ options.overwrite_existing_tracking_urls__boolean }}
{{ options.country_codes_and_tracking_companies__keyval }}
{{ options.default_tracking_company }}
{{ options.notify_customers__boolean }}
{% endcomment %}
{% if options.country_codes_and_tracking_url_templates__keyval == blank and options.default_tracking_url_template == blank %}
{% error "Fill in at least one of 'Country codes and tracking URL templates' and 'Fallback tracking URL template'. :)" %}
{% endif %}
{% for pair in options.country_codes_and_tracking_url_templates__keyval %}
{% unless pair[1] contains "TRACKING_NUMBER" %}
{% error %}{{ "The template for " | append: pair[0] | append: " does not contain the placeholder 'TRACKING_NUMBER'. Please add it!" | json }}{% enderror %}
{% endunless %}
{% endfor %}
{% if options.default_tracking_url_template != blank %}
{% unless options.default_tracking_url_template contains "TRACKING_NUMBER" %}
{% error %}{{ "The default template for does not contain the placeholder 'TRACKING_NUMBER'. Please add it!" | json }}{% enderror %}
{% endunless %}
{% endif %}
{% if event.preview %}
{% capture order_json %}
{
"tags": {{ options.order_tag_to_watch_for__required | json }},
"shipping_address": {
"country_code": {{ options.country_codes_and_tracking_url_templates__keyval.first.first | json }}
},
"fulfillments": [
{
"admin_graphql_api_id": "gid://shopify/Fulfillment/1234567890",
"name": "#1234.1",
"tracking_company": "Other",
"tracking_number": "ABCDEF123456",
"tracking_url": null
}
]
}
{% endcapture %}
{% assign order = order_json | parse_json %}
{% endif %}
{% assign order_tags = order.tags | split: ", " %}
{% if order_tags contains options.order_tag_to_watch_for__required %}
{% assign template = options.country_codes_and_tracking_url_templates__keyval[order.shipping_address.country_code] %}
{% assign template = template | default: options.default_tracking_url_template %}
{% for fulfillment in order.fulfillments %}
{% if fulfillment.tracking_number == blank or fulfillment.tracking_number == "false" %}
{% continue %}
{% endif %}
{% assign new_fulfillment_tracking_url = template | replace: "TRACKING_NUMBER", fulfillment.tracking_number %}
{% assign new_fulfillment_tracking_company = options.country_codes_and_tracking_companies__keyval[order.shipping_address.country_code] | default: options.default_tracking_company | default: fulfillment.tracking_company %}
{% if options.overwrite_existing_tracking_urls__boolean == false and fulfillment.tracking_url != blank %}
{% log message: "Fulfillment already has a tracking URL, and this task is configured not to overwrite, so we're sticking with the existing tracking URL", new_fulfillment_tracking_url: new_fulfillment_tracking_url, fulfillment_tracking_url: fulfillment.tracking_url, fulfillment_name: fulfillment.name, fulfillment_id: fulfillment.id %}
{% assign new_fulfillment_tracking_url = fulfillment.tracking_url %}
{% endif %}
{% if new_fulfillment_tracking_url == fulfillment.tracking_url and new_fulfillment_tracking_company == fulfillment.tracking_company %}
{% log message: "Tracking URL and company is unchanged; skipping this fulfillment", fulfillment_tracking_company: fulfillment.tracking_company, fulfillment_tracking_url: fulfillment.tracking_url, fulfillment_name: fulfillment.name, fulfillment_id: fulfillment.id %}
{% else %}
{% action "shopify" %}
mutation {
fulfillmentTrackingInfoUpdateV2(
fulfillmentId: {{ fulfillment.admin_graphql_api_id | json }}
trackingInfoInput: {
company: {{ new_fulfillment_tracking_company | json }}
number: {{ fulfillment.tracking_number | json }}
url: {{ new_fulfillment_tracking_url | json }}
}
notifyCustomer: {{ options.notify_customers__boolean | json }}
) {
fulfillment {
trackingInfo {
company
number
url
}
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endfor %}
{% endif %}
Mechanic Liquid reference
Default option values
- Order tag to watch for
update-tracking- Country codes and tracking URL templates
{"US" => "https://tools.usps.com/go/TrackConfirmAction_input?qtc_tLabels1=TRACKING_NUMBER", "CA" => "https://www.canadapost.ca/trackweb/en#/search?searchFor=TRACKING_NUMBER", "AU" => "https://auspost.com.au/mypost/track/#/details/TRACKING_NUMBER", "NZ" => "https://www.nzpost.co.nz/tools/tracking/item/TRACKING_NUMBER", "GB" => "http://www.royalmail.com/portal/rm/track?trackNumber=TRACKING_NUMBER"}- Default tracking URL template
http://parcelsapp.com/en/tracking/TRACKING_NUMBER- Overwrite existing tracking URLs
true- Country codes and tracking companies
{"US" => "USPS", "CA" => "Canada Post", "AU" => "Australia Post", "NZ" => "New Zealand Post", "GB" => "Royal Mail"}- Default tracking company
Parcels- Notify customers
true