forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-tag-orders-that-contain-an-out-of-stock-item.json
24 lines (24 loc) · 6.5 KB
/
auto-tag-orders-that-contain-an-out-of-stock-item.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
{
"docs": "This task watches for newly-created orders, and checks each line item for that variant's total inventory quantity. If any are found with an inventory level of 0 or less, the task will add the tag of your choice to the order – and, optionally, will add a tag to the customer and to each product related to an out-of-stock line item.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-tag orders that contain an out of stock item",
"online_store_javascript": null,
"options": {
"apply_this_order_tag__required": "",
"apply_this_customer_tag": null,
"apply_this_product_tag_for_each_out_of_stock_line_item": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"preview_event_definitions": [],
"script": "{% comment %}\n Preferred option order:\n\n {{ options.apply_this_order_tag__required }}\n {{ options.apply_this_customer_tag }}\n {{ options.apply_this_product_tag_for_each_out_of_stock_line_item }}\n{% endcomment %}\n\n{% assign cursor = nil %}\n{% assign out_of_stock_line_items = array %}\n\n{% for n in (0..100) %}\n {% capture query %}\n query {\n order(id: {{ order.admin_graphql_api_id | json }}) {\n id\n tags\n {% if options.apply_this_customer_tag != blank %}\n customer {\n id\n tags\n }\n {% endif %}\n lineItems(\n first: 250\n after: {{ cursor | json }}\n ) {\n pageInfo {\n hasNextPage\n }\n edges {\n cursor\n node {\n name\n product {\n id\n tags\n tracksInventory\n }\n variant {\n inventoryQuantity\n inventoryPolicy\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 \"tags\": [],\n \"customer\": {\n \"id\": \"gid://shopify/Customer/1234567890\",\n \"tags\": []\n },\n \"lineItems\": {\n \"pageInfo\": {\n \"hasNextPage\": false\n },\n \"edges\": [\n {\n \"cursor\": \"eyJsYXN0X2lkIjo0NTEwOTE5MzI3ODA1LCJsYXN0X3ZhbHVlIjo0NTEwOTE5MzI3ODA1fQ==\",\n \"node\": {\n \"name\": \"An out of stock product\",\n \"product\": {\n \"id\": \"gid://shopify/Product/1234567890\",\n \"tags\": [],\n \"tracksInventory\" : true\n },\n \"variant\": {\n \"inventoryQuantity\": -1,\n \"inventoryPolicy\": \"DENY\"\n }\n }\n }\n ]\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% for line_item_edge in result.data.order.lineItems.edges %}\n {% assign variant = line_item_edge.node.variant %}\n {% if variant and variant.inventoryQuantity < 0 and variant.inventoryPolicy == \"DENY\" and line_item_edge.node.product.tracksInventory %}\n {% log %}{{ line_item_edge.node.name | append: \" was purchased while at stock level \" | append: variant.inventoryQuantity | json }}{% endlog %}\n {% assign out_of_stock_line_items[out_of_stock_line_items.size] = line_item_edge.node %}\n {% endif %}\n {% endfor %}\n\n {% if result.data.order.lineItems.pageInfo.hasNextPage %}\n {% assign cursor = result.data.order.lineItems.edges.last.cursor %}\n {% else %}\n {% break %}\n {% endif %}\n{% endfor %}\n\n{% assign mutations = array %}\n\n{% assign order = result.data.order %}\n\n{% if out_of_stock_line_items != empty %}\n {% if order.tags contains options.apply_this_order_tag__required %}\n {% log %}{{ \"This order is already tagged \" | append: options.apply_this_order_tag__required | json }}{% endlog %}\n {% else %}\n {% capture mutation %}\n order: tagsAdd(\n id: {{ order.id | json }}\n tags: {{ options.apply_this_order_tag__required | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n {% endcapture %}\n\n {% assign mutations[mutations.size] = mutation %}\n {% endif %}\n\n {% if options.apply_this_customer_tag != blank %}\n {% if order.customer.tags contains options.apply_this_customer_tag %}\n {% log %}{{ \"This customer is already tagged \" | append: options.apply_this_customer_tag | json }}{% endlog %}\n {% else %}\n {% capture mutation %}\n customer: tagsAdd(\n id: {{ order.customer.id | json }}\n tags: {{ options.apply_this_customer_tag | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n {% endcapture %}\n\n {% assign mutations[mutations.size] = mutation %}\n {% endif %}\n {% endif %}\n\n {% if options.apply_this_product_tag_for_each_out_of_stock_line_item != blank %}\n {% for line_item in out_of_stock_line_items %}\n {% if line_item.product.tags contains options.apply_this_product_tag_for_each_out_of_stock_line_item %}\n {% log %}{{ \"The product for line item \" | append: line_item.name | append: \" is already tagged with \" | append: options.apply_this_product_tag_for_each_out_of_stock_line_item | json }}{% endlog %}\n {% else %}\n {% capture mutation %}\n lineItem{{ forloop.index }}: tagsAdd(\n id: {{ line_item.product.id | json }}\n tags: {{ options.apply_this_product_tag_for_each_out_of_stock_line_item | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n {% endcapture %}\n\n {% assign mutations[mutations.size] = mutation %}\n {% endif %}\n {% endfor %}\n {% endif %}\n{% else %}\n {% log \"No out of stock line items for this order.\" %}\n{% endif %}\n\n{% if mutations != empty %}\n {% action \"shopify\" %}\n mutation {\n {{ mutations | join: newline }}\n }\n {% endaction %}\n{% endif %}",
"subscriptions": [
"shopify/orders/create"
],
"subscriptions_template": "shopify/orders/create",
"tags": [
"Auto-Tag",
"Orders",
"Out of Stock"
]
}