forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-tag-orders-with-their-tracking-numbers.json
23 lines (23 loc) · 4.42 KB
/
auto-tag-orders-with-their-tracking-numbers.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
"docs": "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.\n\nThis 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.\r\n\r\nPlease note: any special characters in the tracking number may be ignored by Shopify.\r\n\r\n[YouTube: Watch the development video!](https://youtu.be/qNsdDaPYl24)",
"halt_action_run_sequence_on_error": false,
"name": "Auto-tag orders with their tracking numbers",
"online_store_javascript": null,
"options": {},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% if event.topic contains \"shopify/fulfillments/\" %}\n {% if event.preview %}\n {% assign fulfillment = hash %}\n {% assign fulfillment[\"tracking_numbers\"] = \"ABC123\" | split: \", \" %}\n {% assign fulfillment[\"order\"] = hash %}\n {% assign fulfillment[\"order\"][\"admin_graphql_api_id\"] = \"gid://shopify/Order/1234567890\" %}\n {% assign fulfillment[\"order\"][\"tags\"] = \"\" %}\n {% endif %}\n\n {% assign order_tags = fulfillment.order.tags | split: \", \" %}\n {% assign tags_to_add = array %}\n\n {% for tracking_number in fulfillment.tracking_numbers %}\n {% if tracking_number != blank %}\n {% unless order_tags contains tracking_number %}\n {% assign tags_to_add[tags_to_add.size] = tracking_number %}\n {% endunless %}\n {% endif %}\n {% endfor %}\n\n {% if tags_to_add != empty %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ fulfillment.order.admin_graphql_api_id | json }}\n tags: {{ tags_to_add | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n{% elsif event.topic == \"mechanic/user/trigger\" %}\n {% capture bulk_operation_query %}\n query {\n orders {\n edges {\n node {\n id\n tags\n fulfillments {\n trackingInfo {\n number\n }\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% action \"shopify\" %}\n mutation {\n bulkOperationRunQuery(\n query: {{ bulk_operation_query | json }}\n ) {\n bulkOperation {\n id\n status\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n{% elsif event.topic == \"mechanic/shopify/bulk_operation\" %}\n {% if event.preview %}\n {% capture objects_json %}\n [\n {\n \"id\": \"gid://shopify/Order/1234567890\",\n \"tags\": [],\n \"fulfillments\": [\n {\n \"trackingInfo\": [\n {\n \"number\": \"ABC123\"\n }\n ]\n }\n ]\n }\n ]\n {% endcapture %}\n\n {% assign bulkOperation = hash %}\n {% assign bulkOperation[\"objects\"] = objects_json | parse_json %}\n {% endif %}\n\n {% for order in bulkOperation.objects %}\n {% assign tags_to_add = array %}\n\n {% for fulfillment in order.fulfillments %}\n {% for trackingInfoObject in fulfillment.trackingInfo %}\n {% if trackingInfoObject.number != blank %}\n {% unless order.tags contains trackingInfoObject.number %}\n {% assign tags_to_add[tags_to_add.size] = trackingInfoObject.number %}\n {% endunless %}\n {% endif %}\n {% endfor %}\n {% endfor %}\n\n {% if tags_to_add != empty %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ order.id | json }}\n tags: {{ tags_to_add | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n {% endfor %}\n{% endif %}",
"subscriptions": [
"shopify/fulfillments/create",
"shopify/fulfillments/update",
"mechanic/user/trigger",
"mechanic/shopify/bulk_operation"
],
"subscriptions_template": "shopify/fulfillments/create\nshopify/fulfillments/update\nmechanic/user/trigger\nmechanic/shopify/bulk_operation",
"tags": [
"Auto-Tag",
"Orders",
"Shipping",
"Tracking"
]
}