forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchive-orders-when-tagged.json
24 lines (24 loc) · 3.39 KB
/
archive-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
24
{
"docs": "Add a tag, have the order automatically archived/closed. Simple. :)\n\nThis task monitors for new and updated orders, watching for the tag of your choice to be added. When the tag is found, this task will make sure the order is archived/closed.\r\n\r\nRun this task manually to scan your older orders, archiving any that match your tag.\r\n\r\nEnable \"Allow manually unarchiving of orders\" to have this task add a metafield to each order, as it archives them. The task will use this metafield as a reminder to _not_ archive those orders in the future, regardless of what happens to them.",
"halt_action_run_sequence_on_error": false,
"name": "Archive orders when tagged",
"online_store_javascript": null,
"options": {
"required_tag__required": "archive-me",
"allow_manual_unarchiving_of_orders__boolean": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% assign metafield_key = task.id | sha256 | slice: 0, 5 | prepend: \"archived-\" %}\n\n{% assign orders = array %}\n{% if event.preview or event.topic contains \"shopify/orders/\" %}\n {% assign orders[0] = order %}\n{% elsif event.topic == \"mechanic/user/trigger\" %}\n {% assign orders = shop.orders.open %}\n{% endif %}\n\n{% for order in orders %}\n {% assign order_tags = order.tags | downcase | split: \", \" %}\n {% assign tag_to_match = options.required_tag__required | downcase | strip %}\n\n {% assign order_qualifies = false %}\n {% if order.closed_at == blank and order_tags contains tag_to_match %}\n {% assign order_qualifies = true %}\n {% endif %}\n\n {% if options.allow_manual_unarchiving_of_orders__boolean %}\n {% if order.metafields.mechanic[metafield_key] %}\n {% assign order_qualifies = false %}\n {% log \"Order has previously been archived, and manual unarchiving is allowed. Skipping.\" %}\n {% endif %}\n {% endif %}\n\n {% if event.preview or order_qualifies %}\n {% action \"shopify\" %}\n mutation {\n orderClose(\n input: {\n id: {{ order.admin_graphql_api_id | json }}\n }\n ) {\n order {\n closed\n closedAt\n }\n userErrors {\n field\n message\n }\n }\n \n {% if options.allow_manual_unarchiving_of_orders__boolean %}\n orderUpdate(\n input: {\n id: {{ order.admin_graphql_api_id | json }}\n metafields: [\n {\n namespace: \"mechanic\"\n key: {{ metafield_key | json }}\n type: \"date_time\"\n value: {{ \"now\" | date: \"%FT%T%:z\" | json }}\n }\n ]\n }\n ) {\n order {\n metafield(\n namespace: \"mechanic\"\n key: {{ metafield_key | json }}\n ) {\n id\n value\n }\n }\n userErrors {\n field\n message\n }\n }\n {% endif %}\n }\n {% endaction %}\n {% endif %}\n{% endfor %}",
"subscriptions": [
"shopify/orders/create",
"shopify/orders/updated",
"mechanic/user/trigger"
],
"subscriptions_template": "shopify/orders/create\nshopify/orders/updated\nmechanic/user/trigger",
"tags": [
"Archive",
"Orders",
"Tag"
]
}