-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathsync-an-inverse-order-tag.json
25 lines (25 loc) · 3.96 KB
/
sync-an-inverse-order-tag.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
25
{
"docs": "Shopify's order group filters allow you to filter by the tags the order contains – but not the tags a order does not contain. Use this task to sync an \"inverse\" tag to all orders who do not have a particular tag, allowing you to filter orders in Shopify by that inverse tag.\n\nTo explain by example, if you've already tagged your wholesale orders with \"wholesale\", configure the \"order tag to watch\" option with \"wholesale\", and the \"order tag to use when missing\" option with \"retail\". This task will automatically tag all non-wholesale orders with \"retail\", and automatically remove that tag from any orders who later receive the tag \"wholesale\". Please note: if you remove the \"retail\" tag, this task will simply re-add it. To remove the \"retail\" tag, you would need to _add_ the \"wholesale\" tag to that order.\r\n\r\nRun this task manually to scan all of the orders in your store. Otherwise, this task will monitor and sync tags for all new and updated orders, as they come through.",
"halt_action_run_sequence_on_error": false,
"name": "Sync an inverse order tag",
"online_store_javascript": null,
"options": {
"order_tag_to_watch__required": "wholesale",
"order_tag_to_use_when_missing__required": "retail"
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% assign orders = array %}\n\n{% if event.topic contains \"shopify/orders/\" %}\n {% if event.preview %}\n {% assign order = hash %}\n {% assign order[\"admin_graphql_api_id\"] = \"gid://shopify/order/1234567890\" %}\n {% assign order[\"tags\"] = \"\" %}\n {% endif %}\n\n {% assign order_node = hash %}\n {% assign order_node[\"id\"] = order.admin_graphql_api_id %}\n {% assign order_node[\"tags\"] = order.tags | split: \", \" %}\n {% assign orders[0] = order_node %}\n{% elsif event.topic == \"mechanic/user/trigger\" %}\n {% capture bulk_operation_query %}\n query {\n orders {\n edges {\n node {\n id\n tags\n }\n }\n }\n }\n {% endcapture %}\n\n {% action \"shopify\" %}\n mutation {\n bulkOperationRunQuery(\n query: {{ bulk_operation_query | json }}\n ) {\n bulkOperation {\n id\n status\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n{% elsif event.topic == \"mechanic/shopify/bulk_operation\" %}\n {% if event.preview %}\n {% capture objects_jsonl %}\n {\"id\":\"gid://shopify/order/1234567890\",\"tags\": []}\n {% endcapture %}\n\n {% assign bulkOperation = hash %}\n {% assign bulkOperation[\"objects\"] = objects_jsonl | parse_jsonl %}\n {% endif %}\n\n {% assign orders = bulkOperation.objects %}\n{% endif %}\n\n{% for order in orders %}\n {% assign add_tag = false %}\n {% assign remove_tag = false %}\n\n {% if order.tags contains options.order_tag_to_watch__required %}\n {% if order.tags contains options.order_tag_to_use_when_missing__required %}\n {% assign remove_tag = true %}\n {% endif %}\n {% else %}\n {% unless order.tags contains options.order_tag_to_use_when_missing__required %}\n {% assign add_tag = true %}\n {% endunless %}\n {% endif %}\n\n {% if add_tag or remove_tag %}\n {% action \"shopify\" %}\n mutation {\n tags{% if add_tag %}Add{% else %}Remove{% endif %}(\n id: {{ order.id | json }}\n tags: {{ options.order_tag_to_use_when_missing__required | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n{% endfor %}",
"subscriptions": [
"shopify/orders/create",
"shopify/orders/updated",
"mechanic/user/trigger",
"mechanic/shopify/bulk_operation"
],
"subscriptions_template": "shopify/orders/create\nshopify/orders/updated\nmechanic/user/trigger\nmechanic/shopify/bulk_operation",
"tags": [
"Auto-Tag",
"Orders",
"Sync"
]
}