forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-fulfill-items-that-dont-require-shipping.json
21 lines (21 loc) · 7.88 KB
/
auto-fulfill-items-that-dont-require-shipping.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"docs": "Useful for digital products, memberships, or anything else that needs to be fulfilled instantly. This task watches for paid orders, and auto-fulfills all line items that don't require shipping.\n\nOptionally, choose to ignore products with a specific tag, and wait until other shippable items are fulfilled, if any.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-fulfill items that don't require shipping",
"online_store_javascript": null,
"options": {
"wait_until_any_other_shippable_items_are_fulfilled__boolean": null,
"ignore_products_with_this_tag": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% comment %}\n Preferred option order:\n\n {{ options.wait_until_any_other_shippable_items_are_fulfilled__boolean }}\n {{ options.ignore_products_with_this_tag }}\n{% endcomment %}\n\n{% capture query %}\n query {\n order(id: {{ order.admin_graphql_api_id | json }}) {\n id\n fulfillmentOrders(\n first: 10\n query: \"status:open OR status:in_progress\"\n ) {\n edges {\n node {\n id\n assignedLocation {\n location {\n id\n }\n }\n lineItems(first: 30) {\n edges {\n node {\n id\n remainingQuantity\n lineItem {\n requiresShipping\n product {\n tags\n }\n }\n }\n }\n }\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 \"fulfillmentOrders\": {\n \"edges\": [\n {\n \"node\": {\n \"id\": \"gid://shopify/FulfillmentOrder/1234567890\",\n \"assignedLocation\": {\n \"location\": {\n \"id\": \"gid://shopify/Location/1234567890\"\n }\n },\n \"lineItems\": {\n \"edges\": [\n {\n \"node\": {\n \"id\": \"gid://shopify/FulfillmentOrderLineItem/1234567890\",\n \"remainingQuantity\": 1,\n \"lineItem\": {\n \"requiresShipping\": false\n }\n }\n },\n {\n \"node\": {\n \"id\": \"gid://shopify/FulfillmentOrderLineItem/2345678901\",\n \"remainingQuantity\": 2,\n \"lineItem\": {\n \"requiresShipping\": false,\n \"product\": {\n \"tags\": [\n {{ options.ignore_products_with_this_tag | json }}\n ]\n }\n }\n }\n }\n ]\n }\n }\n },\n {\n \"node\": {\n \"id\": \"gid://shopify/FulfillmentOrder/3456789012\",\n \"assignedLocation\": {\n \"location\": {\n \"id\": \"gid://shopify/Location/3456789012\"\n }\n },\n \"lineItems\": {\n \"edges\": [\n {\n \"node\": {\n \"id\": \"gid://shopify/FulfillmentOrderLineItem/3456789012\",\n \"remainingQuantity\": 0,\n \"lineItem\": {\n \"requiresShipping\": true\n }\n }\n }\n ]\n }\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.edges | map: \"node\" %}\n\n{% if fulfillment_orders == blank %}\n {% log \"There are no open fulfillment orders to fulfill on this order.\" %}\n {% break %}\n{% endif %}\n\n{% comment %}\n NOTE: fulfillments can only be created for one location at a time, so need to group fulfillment orders by location\n{% endcomment %}\n\n{% assign fulfillment_orders_by_location = hash %}\n{% assign has_unfulfilled_shippable_items = nil %}\n\n{% for fulfillment_order in fulfillment_orders %}\n {% assign fulfillment_order_data = hash %}\n {% assign fulfillment_order_data[\"fulfillment_order_id\"] = fulfillment_order.id %}\n\n {% assign fulfillment_order_line_items = fulfillment_order.lineItems.edges | map: \"node\" %}\n\n {% for fulfillment_order_line_item in fulfillment_order_line_items %}\n {% if options.ignore_products_with_this_tag != blank %}\n {% if fulfillment_order_line_item.lineItem.product.tags contains options.ignore_products_with_this_tag %}\n {% continue %}\n {% endif %}\n {% endif %}\n\n {% if fulfillment_order_line_item.remainingQuantity > 0 %}\n {% if fulfillment_order_line_item.lineItem.requiresShipping %}\n {% assign has_unfulfilled_shippable_items = true %}\n\n {% else %}\n {% assign fulfillment_order_data[\"unfulfilled_line_items\"]\n = fulfillment_order_data[\"unfulfilled_line_items\"]\n | default: array\n | push: fulfillment_order_line_item\n %}\n {% endif %}\n {% endif %}\n {% endfor %}\n\n {% if fulfillment_order_data.unfulfilled_line_items != blank %}\n {% assign fulfillment_orders_by_location[fulfillment_order.assignedLocation.location.id]\n = fulfillment_orders_by_location[fulfillment_order.assignedLocation.location.id]\n | default: array\n | push: fulfillment_order_data\n %}\n {% endif %}\n{% endfor %}\n\n{% if options.wait_until_any_other_shippable_items_are_fulfilled__boolean and has_unfulfilled_shippable_items %}\n {% log \"Unfulfilled shippable items exist on this order and the 'Wait until any other shippable items are fulfilled' option is checked; no auto fulfillments will be made in this task run.\" %}\n {% break %}\n{% endif %}\n\n{% for keyval in fulfillment_orders_by_location %}\n {% action \"shopify\" %}\n mutation {\n fulfillmentCreateV2(\n fulfillment: {\n lineItemsByFulfillmentOrder: [\n {% for fulfillment_order_data in keyval[1] %}\n {\n fulfillmentOrderId: {{ fulfillment_order_data.fulfillment_order_id | json }}\n fulfillmentOrderLineItems: [\n {% for unfulfilled_line_item in fulfillment_order_data.unfulfilled_line_items %}\n {\n id: {{ unfulfilled_line_item.id | json }}\n quantity: {{ unfulfilled_line_item.remainingQuantity }}\n }\n {% endfor %}\n ]\n }\n {% endfor %}\n ]\n notifyCustomer: false\n }\n ) {\n fulfillment {\n id\n status\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n{% endfor %}",
"subscriptions": [
"shopify/orders/paid"
],
"subscriptions_template": "shopify/orders/paid\n{% if options.wait_until_any_other_shippable_items_are_fulfilled__boolean %}\n shopify/orders/partially_fulfilled\n shopify/orders/fulfilled\n{% endif %}",
"tags": [
"Fulfillment",
"Orders"
]
}