forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-tag-orders-by-shipment-status.json
41 lines (41 loc) · 7.23 KB
/
auto-tag-orders-by-shipment-status.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{
"docs": "Easily segment your orders by shipment status. Tagging your orders as \"Delivered\", or \"Out for delivery\", or \"Attempted delivery\", or any other status, allows you to stay on top of your order flow. And, adding these tags makes it easier to wire up additional automation, keeping the connection live between your customers and their orders.\n\nThis task monitors fulfillments for your orders. Whenever a fulfillment is updated, this task removes any shipment-related tags that might already be on the order, then adds whatever tag is relevant for the current shipment status. If there are multiple fulfillments for the order, and they have different statuses, this task will add more than one tag. Use the \"Run task\" button to scan all orders. To process orders older than 60 days, [enable \"Read all orders\"](https://docs.usemechanic.com/article/375-enabling-read-all-orders).\n\nThis task comes pre-configured with friendly tags, but feel free to update the tag names on the right-hand side. Do not modify the labels on the left! These correspond directly to shipment statuses as they're recorded by Shopify.\n\nFor a reference on Shopify's fulfillment statuses, see the [FulfillmentDisplayStatus documentation](https://shopify.dev/docs/admin-api/graphql/reference/shipping-and-fulfillment/fulfillmentdisplaystatus). (More details may be available in the [REST documentation for Fulfillment.shipment_status](https://shopify.dev/docs/admin-api/rest/reference/shipping-and-fulfillment/fulfillment#shipment-status-property-2021-04).)\n\nThe \"Limit to orders matching this query for manual runs\" option uses the same query syntax as the \"Orders\" section of the Shopify admin area. For example, to only include paid orders, use this query:\n\nfinancial_status:paid",
"halt_action_run_sequence_on_error": false,
"name": "Auto-tag orders by shipment status",
"online_store_javascript": null,
"options": {
"shipping_statuses_and_tags__keyval_required": {
"ATTEMPTED_DELIVERY": "Delivery attempted",
"CANCELED": "Fulfillment Canceled",
"CONFIRMED": "Delivery confirmed",
"DELIVERED": "Delivery complete",
"FAILURE": "Delivery failed",
"FULFILLED": "Fulfilled",
"IN_TRANSIT": "Delivery in transit",
"LABEL_PRINTED": "Shipping label printed",
"LABEL_PURCHASED": "Shipping label purchased",
"LABEL_VOIDED": "Shipping label voided",
"MARKED_AS_FULFILLED": "Marked as fulfilled",
"NOT_DELIVERED": "Not delivered",
"OUT_FOR_DELIVERY": "Out for delivery",
"PICKED_UP": "Picked up",
"READY_FOR_PICKUP": "Delivery ready for pickup",
"SUBMITTED": "Submitted"
},
"limit_to_orders_matching_this_query_for_manual_runs": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% comment %}\nOptions ordering:\n {{ options.shipping_statuses_and_tags__keyval_required }}\n {{ options.limit_to_orders_matching_this_query_for_manual_runs }}\n{% endcomment %}\n\n{% if event.topic contains \"shopify/fulfillments/\" %}\n {% capture query %}\n query {\n order(id: {{ fulfillment.order.admin_graphql_api_id | json }}) {\n id\n tags\n fulfillments {\n displayStatus\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 \"displayStatus\": \"IN_TRANSIT\"\n },\n {\n \"displayStatus\": \"CANCELED\"\n },\n {\n \"displayStatus\": \"OUT_FOR_DELIVERY\"\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign orders = array %}\n {% assign orders[0] = result.data.order %}\n\n{% elsif event.topic == \"mechanic/user/trigger\" %}\n {% assign orders_query = nil %}\n\n {% if options.limit_to_orders_matching_this_query_for_manual_runs != blank %}\n {% assign orders_query = options.limit_to_orders_matching_this_query_for_manual_runs %}\n {% endif %}\n\n {% capture bulk_operation_query %}\n query {\n orders(\n query: {{ orders_query | json }}\n ) {\n edges {\n node {\n id\n tags\n fulfillments {\n displayStatus\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\n{% elsif event.topic == \"mechanic/shopify/bulk_operation\" %}\n {% if event.preview %}\n {% capture objects_jsonl %}\n {\"id\":\"gid:\\/\\/shopify\\/Order\\/1234567890\",\"tags\":[],\"fulfillments\":[{\"displayStatus\":\"IN_TRANSIT\"},{\"displayStatus\":\"CANCELED\"},{\"displayStatus\":\"OUT_FOR_DELIVERY\"}]}\n {\"id\":\"gid:\\/\\/shopify\\/Order\\/2345678901\",\"tags\":[],\"fulfillments\":[{\"displayStatus\":\"DELIVERED\"}]}\n {% endcapture %}\n\n {% assign bulkOperation = hash %}\n {% assign bulkOperation[\"objects\"] = objects_jsonl | parse_jsonl %}\n {% endif %}\n\n {% assign orders = bulkOperation.objects %}\n{% endif %}\n\n{% for order in orders %}\n {% assign tags_to_add = array %}\n {% assign tags_to_remove = array %}\n\n {% assign display_statuses = order.fulfillments | map: \"displayStatus\" %}\n\n {% for keyval in options.shipping_statuses_and_tags__keyval_required %}\n {% assign shipping_status = keyval.first %}\n {% assign tag = keyval.last %}\n\n {% if display_statuses contains shipping_status %}\n {% unless order.tags contains tag %}\n {% assign tags_to_add[tags_to_add.size] = tag %}\n {% endunless %}\n {% elsif order.tags contains tag %}\n {% assign tags_to_remove[tags_to_remove.size] = tag %}\n {% endif %}\n {% endfor %}\n\n {% if tags_to_add != empty or tags_to_remove != empty %}\n {% action \"shopify\" %}\n mutation {\n {% if tags_to_add != empty %}\n tagsAdd(\n id: {{ order.id | json }}\n tags: {{ tags_to_add | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n {% endif %}\n\n {% if tags_to_remove != empty %}\n tagsRemove(\n id: {{ order.id | json }}\n tags: {{ tags_to_remove | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n {% endif %}\n }\n {% endaction %}\n {% endif %}\n{% endfor %}",
"subscriptions": [
"shopify/fulfillments/create",
"shopify/fulfillments/update",
"mechanic/user/trigger",
"mechanic/shopify/bulk_operation"
],
"subscriptions_template": "shopify/fulfillments/create\r\nshopify/fulfillments/update\r\nmechanic/user/trigger\r\nmechanic/shopify/bulk_operation",
"tags": [
"Auto-Tag",
"Shipping"
]
}