-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-tag-orders-based-on-shipping-method.json
27 lines (27 loc) · 6.13 KB
/
auto-tag-orders-based-on-shipping-method.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
26
27
{
"docs": "Quickly identify and sort orders based on their shipping method, using automatic order tags. This task supports auto-tagging incoming orders, and can be run manually to tag historical orders.\n\nTo use this task, populate the \"Shipping method titles and tags\" list with shipping method titles on the left, and order tags to apply on the right. In Shopify, shipping method titles are configured as \"rates\"; you can find yours by navigating to Settings > Shipping, then looking at the rates within each shipping zone.\r\n\r\nThis task will run automatically on incoming orders. Use the \"Run task\" button to scan and tag older orders. To process orders older than 60 days, [enable \"Read all orders\"](https://help.usemechanic.com/tutorials/enabling-read_all_orders).\r\n\r\nEnable \"Allow partial matches for shipping method titles\" for more flexibility. With this option enabled, Mechanic will look for the first value in the \"Shipping method titles and tags\" list that is found within the order's shipping method title, and apply the corresponding tag.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-tag orders based on shipping method",
"online_store_javascript": null,
"options": {
"shipping_method_titles_and_tags__keyval_required": {
"Shipping Alpha": "alpha",
"Shipping Beta": "beta",
"Shipping Gamma": "gamma"
},
"allow_partial_matches_for_shipping_method_titles__boolean": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% if event.preview %}\n {% for keyval in options.shipping_method_titles_and_tags__keyval_required %}\n {% assign tag = keyval[1] %}\n {% break %}\n {% endfor %}\n\n {% capture mutation %}\n mutation {\n tagsAdd(\n id: \"gid://shopify/Order/1234567890\"\n tags: {{ tag | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endcapture %}\n\n {% action \"shopify\" mutation %}\n{% elsif event.topic contains \"shopify/orders/\" %}\n {% if options.allow_partial_matches_for_shipping_method_titles__boolean %}\n {% for keyval in options.shipping_method_titles_and_tags__keyval_required %}\n {% if order.shipping_lines.first.title contains keyval[0] %}\n {% assign tag = keyval[1] %}\n {% break %}\n {% endif %}\n {% endfor %}\n {% else %}\n {% assign tag = options.shipping_method_titles_and_tags__keyval_required[order.shipping_lines.first.title] %}\n {% endif %}\n\n {% assign order_tags = order.tags | split: \", \" %}\n\n {\"log\": {{ order.shipping_lines.first.title | json | prepend: \"Shipping method title for this order: \" | json }}}\n\n {% if tag == nil %}\n {\"log\": \"No tag to add for this order; nothing to do.\"}\n {% elsif order_tags contains tag %}\n {\"log\": \"Order is already tagged {{ tag | json }}; nothing to do.\"}\n {% else %}\n {% capture mutation %}\n mutation {\n tagsAdd(\n id: {{ order.admin_graphql_api_id | default: \"gid://shopify/Order/1234567890\" | json }}\n tags: {{ tag | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endcapture %}\n\n {% action \"shopify\" mutation %}\n {% endif %}\n{% elsif event.topic == \"mechanic/user/trigger\" %}\n {% assign query_parts = array %}\n {% for keyval in options.shipping_method_titles_and_tags__keyval_required %}\n {% assign query_parts[query_parts.size] = keyval[1] | json | prepend: \"-tag:\" %}\n {% endfor %}\n {% assign query = query_parts | uniq | join: \" AND \" %}\n\n {\"log\": {{ query | json | prepend: \"Searching for orders matching \" | json }}}\n\n {% assign cursor = nil %}\n {% for n in (0..100) %}\n {% capture orders_query %}\n query {\n orders(\n first: 250\n after: {{ cursor | json }}\n \n {% comment %}\n As of 2019-07-10, negative tag specifiers can result in an\n empty set of pages, *and* hasNextPage being true. Until that's\n fixed, we disable the query, and just deal with having to go\n through all orders. :)\n\n query: {{ query | json }}\n {% endcomment %}\n ) {\n pageInfo {\n hasNextPage\n }\n edges {\n cursor\n node {\n id\n tags\n shippingLine {\n title\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign orders_result = orders_query | shopify %}\n\n {% for edge in orders_result.data.orders.edges %}\n {% assign order_node = edge.node %}\n {% assign tag = nil %}\n\n {% if options.allow_partial_matches_for_shipping_method_titles__boolean %}\n {% for keyval in options.shipping_method_titles_and_tags__keyval_required %}\n {% if order_node.shippingLine.title contains keyval[0] %}\n {% assign tag = keyval[1] %}\n {% break %}\n {% endif %}\n {% endfor %}\n {% else %}\n {% assign tag = options.shipping_method_titles_and_tags__keyval_required[order_node.shippingLine.title] %}\n {% endif %}\n\n {% if tag %}\n {% unless order_node.tags contains tag %}\n {% capture mutation %}\n mutation {\n tagsAdd(\n id: {{ order_node.id | json }}\n tags: {{ tag | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endcapture %}\n\n {% action \"shopify\" mutation %}\n {% endunless %}\n {% endif %}\n {% endfor %}\n\n {% if orders_result.data.orders.pageInfo.hasNextPage %}\n {% assign cursor = orders_result.data.orders.edges.last.cursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n{% endif %}",
"subscriptions": [
"shopify/orders/create",
"mechanic/user/trigger"
],
"subscriptions_template": "shopify/orders/create\nmechanic/user/trigger",
"tags": [
"Auto-Tag",
"Orders",
"Shipping"
]
}