-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-fulfill-items-that-dont-require-shipping.json
23 lines (23 loc) · 9.39 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
22
23
{
"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 include or exclude products by tag, and wait until other shippable items are fulfilled, if any.\n\nNote: exclusion tags on a product will take priority over inclusion tags.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-fulfill items that don't require shipping",
"online_store_javascript": null,
"options": {
"include_products_with_any_of_these_tags__array": null,
"exclude_products_with_any_of_these_tags__array": null,
"wait_until_any_other_shippable_items_are_fulfilled__boolean": false
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% assign inclusion_tags = options.include_products_with_any_of_these_tags__array %}\n{% assign exclusion_tags = options.exclude_products_with_any_of_these_tags__array %}\n{% assign wait_until_any_other_shippable_items_are_fulfilled = options.wait_until_any_other_shippable_items_are_fulfilled__boolean %}\n\n{% comment %}\n -- get all open or in progress fulfillment orders\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 nodes {\n id\n assignedLocation {\n location {\n id\n }\n }\n lineItems(first: 30) {\n nodes {\n id\n inventoryItemId\n remainingQuantity\n requiresShipping\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 \"nodes\": [\n {\n \"id\": \"gid://shopify/FulfillmentOrder/1234567890\",\n \"assignedLocation\": {\n \"location\": {\n \"id\": \"gid://shopify/Location/1234567890\"\n }\n },\n \"lineItems\": {\n \"nodes\": [\n {\n \"id\": \"gid://shopify/FulfillmentOrderLineItem/1234567890\",\n \"inventoryItemId\": \"gid://shopify/InventoryItem/1234567890\",\n \"remainingQuantity\": 1,\n \"requiresShipping\": false\n },\n {\n \"id\": \"gid://shopify/FulfillmentOrderLineItem/2345678901\",\n \"inventoryItemId\": \"gid://shopify/InventoryItem/2345678901\",\n \"remainingQuantity\": 2,\n \"requiresShipping\": false\n }\n ]\n }\n }\n ]\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n{% endif %}\n\n{% assign fulfillment_orders = result.data.order.fulfillmentOrders.nodes %}\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 {% for fulfillment_order_line_item in fulfillment_order.lineItems.nodes %}\n {% comment %}\n -- skip items that do not require shipping, but set flag if any are unfulfilled\n {% endcomment %}\n\n {% if fulfillment_order_line_item.requiresShipping %}\n {% if fulfillment_order_line_item.remainingQuantity > 0 %}\n {% assign has_unfulfilled_shippable_items = true %}\n {% endif %}\n\n {% continue %}\n {% endif %}\n\n {% if inclusion_tags != blank or exclusion_tags != blank %}\n {% assign has_exclusion_tag = nil %}\n {% assign has_inclusion_tag = nil %}\n\n {% comment %}\n -- use the inventory item Id to query for product tags; product access from fulfillment order line items has been deprecated\n {% endcomment %}\n\n {% capture query %}\n query {\n inventoryItem(id: {{ fulfillment_order_line_item.inventoryItemId | json }}) {\n variant {\n product {\n tags\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 \"inventoryItem\": {\n \"variant\": {\n \"product\": {\n \"tags\": {{ inclusion_tags.first | json }}\n }\n }\n }\n }\n }\n {% endcapture %}\n \n {% assign result = result_json | parse_json %}\n {% endif %}\n \n {% assign product_tags = result.data.inventoryItem.variant.product.tags %}\n\n {% for exclusion_tag in exclusion_tags %}\n {% if product_tags contains exclusion_tag %}\n {% assign has_exclusion_tag = true %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% for inclusion_tag in inclusion_tags %}\n {% if product_tags contains inclusion_tag %}\n {% assign has_inclusion_tag = true %}\n {% break %}\n {% endif %} \n {% endfor %}\n\n {% comment %}\n -- exclusion tags have priority over inclusion tags\n {% endcomment %}\n\n {% if has_exclusion_tag %}\n {% log\n message: \"This line item's product has at least one of the configured exclusion tags; skipping\",\n fulfillment_order_line_item: fulfillment_order_line_item,\n product_tags: product_tags,\n exclusion_tags: exclusion_tags\n %}\n {% continue %}\n\n {% elsif inclusion_tags != blank %}\n {% unless has_inclusion_tag %}\n {% log\n message: \"This line item's product does not contain any of the configured inclusion tags; skipping\",\n fulfillment_order_line_item: fulfillment_order_line_item,\n product_tags: product_tags,\n inclusion_tags: inclusion_tags\n %}\n {% continue %}\n {% endunless %}\n {% endif %}\n {% endif %}\n\n {% comment %}\n -- save unfulfilled line items that do not require shipping\n {% endcomment %}\n\n {% if fulfillment_order_line_item.remainingQuantity > 0 %}\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 {% endfor %}\n\n {% comment %}\n -- group unfulfilled line items by location for fulfillment\n {% endcomment %}\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 wait_until_any_other_shippable_items_are_fulfilled 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",
"mechanic/user/order"
],
"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 %}\nmechanic/user/order",
"tags": [
"Fulfillment",
"Orders"
]
}