forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-tag-fulfilled-orders-with-their-carriers.json
20 lines (20 loc) · 2.47 KB
/
auto-tag-fulfilled-orders-with-their-carriers.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
"docs": "When an order is fulfilled, this task will scan the tracking information for each fulfillment. The name of each tracking company on file – i.e. the fulfillment carrier – will then be added to the order as a tag.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-tag fulfilled orders with their carriers",
"online_store_javascript": null,
"options": {},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% if event.preview %}\n {% assign order = hash %}\n {% assign order[\"admin_graphql_api_id\"] = \"gid://shopify/Order/1234567890\" %}\n{% endif %}\n\n{% capture query %}\n query {\n order(id: {{ order.admin_graphql_api_id | json }}) {\n id\n tags\n fulfillments {\n name\n trackingInfo {\n company\n }\n }\n }\n }\n{% endcapture %}\n\n{% assign result = query | shopify %}\n\n{% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"order\": {\n \"id\": \"gid://shopify/Order/1234567890\",\n \"tags\": [],\n \"fulfillments\": [\n {\n \"name\": \"#1234-F1\",\n \"trackingInfo\": [\n {\n \"company\": \"Example Logistics 1\"\n }\n ]\n },\n {\n \"name\": \"#1234-F2\",\n \"trackingInfo\": [\n {\n \"company\": null\n }\n ]\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n{% endif %}\n\n{% assign companies = result.data.order.fulfillments | map: \"trackingInfo\" | map: \"company\" %}\n{% assign tags_to_add = array %}\n\n{% for company in companies %}\n {% if company == blank or result.data.order.tags contains company %}\n {% continue %}\n {% endif %}\n\n {% assign tags_to_add[tags_to_add.size] = company %}\n{% endfor %}\n\n{% if tags_to_add != empty %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ 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 %}",
"subscriptions": [
"shopify/orders/fulfilled"
],
"subscriptions_template": "shopify/orders/fulfilled",
"tags": [
"Auto-Tag",
"Fulfillment",
"Orders",
"Shipping"
]
}