-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-fulfill-orders-when-tagged.json
23 lines (23 loc) · 4.1 KB
/
auto-fulfill-orders-when-tagged.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": "This task monitors order tags, and when a certain tag is added to an order, it auto-fulfills all unfulfilled items in the order. Optionally, you can choose to notify customers when their order is fulfilled by this task.\n\n__Note__: If a qualifying order has open fulfillments assigned to multiple locations or having multiple delivery methods when this task fulfills the order, then the customer will receive multiple notifications if that option is enabled in the task.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-fulfill orders when tagged",
"online_store_javascript": null,
"options": {
"order_tag_to_watch_for__required": null,
"ignore_if_this_order_tag_is_found": null,
"notify_customer_on_fulfillment__boolean": false
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% capture query %}\n query {\n order(id: {{ order.admin_graphql_api_id | json }}) {\n id\n tags\n fulfillmentOrders(\n first: 20\n query: \"status:open\"\n ) {\n nodes {\n id\n assignedLocation {\n name\n }\n deliveryMethod {\n methodType\n }\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\": {{ options.order_tag_to_watch_for__required | json }},\n \"fulfillmentOrders\": {\n \"nodes\": [\n {\n \"id\": \"gid://shopify/FulfillmentOrder/1234567890\",\n \"assignedLocation\": {\n \"name\": \"ACME Warehouse\"\n },\n \"deliveryMethod\": {\n \"methodType\": \"SHIPPING\"\n }\n }\n ]\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n{% endif %}\n\n{% assign order = result.data.order %}\n{% assign fulfillment_orders = order.fulfillmentOrders.nodes %}\n\n{% if fulfillment_orders != blank and order.tags contains options.order_tag_to_watch_for__required %}\n {% if options.ignore_if_this_order_tag_is_found != blank and order.tags contains options.ignore_if_this_order_tag_is_found %}\n {% log message: \"Order had the tag to watch for, but it also had the ignore tag. Skipping.\", order_tags: order.tags, task_options: options %}\n {% break %}\n {% endif %}\n\n {% comment %}\n Note: fulfillments cannot be created with fulfillment orders at different locations or delivery types; so separate them out\n {% endcomment %}\n\n {% assign fulfillment_order_ids_by_location_and_type = hash %}\n\n {% for fulfillment_order in fulfillment_orders %}\n {% assign location_and_type\n = fulfillment_order.assignedLocation.name\n | append: \"|\"\n | append: fulfillment_order.deliveryMethod.methodType\n %}\n {% assign fulfillment_order_ids_by_location_and_type[location_and_type]\n = fulfillment_order_ids_by_location_and_type[location_and_type]\n | default: array\n | push: fulfillment_order.id\n %}\n {% endfor %}\n\n {% for keyval in fulfillment_order_ids_by_location_and_type %}\n {% action \"shopify\" %}\n mutation {\n fulfillmentCreateV2(\n fulfillment: {\n lineItemsByFulfillmentOrder: [\n {% for fulfillment_order_id in keyval[1] %}\n { fulfillmentOrderId: {{ fulfillment_order_id | json }} }\n {% endfor %}\n ]\n notifyCustomer: {{ options.notify_customer_on_fulfillment__boolean | json }}\n }\n ) {\n fulfillment {\n id\n status\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endfor %}\n{% endif %}\n",
"subscriptions": [
"shopify/orders/updated"
],
"subscriptions_template": "shopify/orders/updated",
"tags": [
"Fulfillment",
"Orders",
"Tag"
]
}