Auto-tag orders with their tracking numbers
Tracking numbers aren't generally searchable within Shopify. Use this task to fix that, by copying all fulfillment tracking numbers over to the order itself, as (searchable!) order tags.
Try this taskWhen it runs
- Occurs whenever a fulfillment is created
- Occurs whenever a fulfillment is updated
- Occurs when a user manually triggers the task
- Occurs when a bulk operation is completed
Documentation
Tracking numbers aren't generally searchable within Shopify. Use this task to fix that, by copying all fulfillment tracking numbers over to the order itself, as (searchable!) order tags.
This task runs when fulfillments are created or updated, adding the fulfillment's tracking number(s) to the order's tags. Run this task manually to scan and tag all of your store's existing orders.
Please note: any special characters in the tracking number may be ignored by Shopify.
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
mechanic/user/trigger
mechanic/shopify/bulk_operation
How task subscriptions work
Task code
{% if event.topic contains "shopify/fulfillments/" %}
{% if event.preview %}
{% assign fulfillment = hash %}
{% assign fulfillment["tracking_numbers"] = "ABC123" | split: ", " %}
{% assign fulfillment["order"] = hash %}
{% assign fulfillment["order"]["admin_graphql_api_id"] = "gid://shopify/Order/1234567890" %}
{% assign fulfillment["order"]["tags"] = "" %}
{% endif %}
{% assign order_tags = fulfillment.order.tags | split: ", " %}
{% assign tags_to_add = array %}
{% for tracking_number in fulfillment.tracking_numbers %}
{% if tracking_number != blank %}
{% unless order_tags contains tracking_number %}
{% assign tags_to_add[tags_to_add.size] = tracking_number %}
{% endunless %}
{% endif %}
{% endfor %}
{% if tags_to_add != empty %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ fulfillment.order.admin_graphql_api_id | json }}
tags: {{ tags_to_add | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% elsif event.topic == "mechanic/user/trigger" %}
{% capture bulk_operation_query %}
query {
orders {
edges {
node {
id
tags
fulfillments {
trackingInfo {
number
}
}
}
}
}
}
{% endcapture %}
{% action "shopify" %}
mutation {
bulkOperationRunQuery(
query: {{ bulk_operation_query | json }}
) {
bulkOperation {
id
status
}
userErrors {
field
message
}
}
}
{% endaction %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
{% if event.preview %}
{% capture objects_json %}
[
{
"id": "gid://shopify/Order/1234567890",
"tags": [],
"fulfillments": [
{
"trackingInfo": [
{
"number": "ABC123"
}
]
}
]
}
]
{% endcapture %}
{% assign bulkOperation = hash %}
{% assign bulkOperation["objects"] = objects_json | parse_json %}
{% endif %}
{% for order in bulkOperation.objects %}
{% assign tags_to_add = array %}
{% for fulfillment in order.fulfillments %}
{% for trackingInfoObject in fulfillment.trackingInfo %}
{% if trackingInfoObject.number != blank %}
{% unless order.tags contains trackingInfoObject.number %}
{% assign tags_to_add[tags_to_add.size] = trackingInfoObject.number %}
{% endunless %}
{% endif %}
{% endfor %}
{% endfor %}
{% if tags_to_add != empty %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ order.id | json }}
tags: {{ tags_to_add | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endfor %}
{% endif %}
Mechanic Liquid reference