-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathauto-tag-orders-with-their-line-item-properties.json
24 lines (24 loc) · 6.53 KB
/
auto-tag-orders-with-their-line-item-properties.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": "Use this task to tag incoming orders with all of the line item properties found on the order. Optionally, configure a list of property names to be included, or a list of property names to be excluded. This task can also be run manually to scan and tag historical orders.\n\nChange the \"Property name and value separator\" to change the way tags are built. Using a dash results in \"Name-Value\", an underscore results in \"Name_Value\", and a colon with a space yields \"Name: Value\". The task preview conveniently shows what a sample tag combo will look like.\n\nNotes:\n- Configuring any property names to include means the task will ignore the property names to exclude setting entirely.\n- Because line item properties cannot change after order creation, this task does not remove any tags from orders.\n- Shopify limits order tags to 40 characters in length. If a tag combo exceeds this limit it will not be set on the order.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-tag orders with their line item properties",
"online_store_javascript": null,
"options": {
"property_name_and_value_separator__required": "_",
"include_only_these_property_names__array": [],
"exclude_these_property_names__array": []
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% assign separator = options.property_name_and_value_separator__required %}\n{% assign include_only_these_property_names = options.include_only_these_property_names__array %}\n{% assign exclude_these_property_names = options.exclude_these_property_names__array %}\n\n{% assign orders = array %}\n\n{% if event.topic == \"shopify/orders/create\" %}\n {% if event.preview %}\n {% assign sample_tag = \"Name\" | append: separator | append: \"Value\" %}\n {% action \"echo\" sample_tag: sample_tag %}\n {% endif %}\n\n {% capture query %}\n query {\n order(id: {{ order.admin_graphql_api_id | json }}) {\n id\n name\n tags\n lineItems(first: 100) {\n edges {\n node {\n id\n customAttributes {\n key\n value\n }\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% assign orders[0] = result.data.order %}\n\n{% elsif event.topic == \"mechanic/user/trigger\" %}\n {% assign cursor = nil %}\n\n {% for n in (1..2500) %}\n {% capture query %}\n query {\n orders(\n first: 4\n after: {{ cursor | json }}\n reverse: true\n ) {\n pageInfo {\n hasNextPage\n }\n edges {\n cursor\n node {\n id\n name\n tags\n lineItems(first: 100) {\n edges {\n node {\n customAttributes {\n key\n value\n }\n }\n }\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% assign orders_batch = result.data.orders.edges | map: \"node\" %}\n {% assign orders = orders | concat: orders_batch %}\n\n {% if result.data.orders.pageInfo.hasNextPage %}\n {% assign cursor = result.data.orders.edges.last.cursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n{% endif %}\n\n{% if event.preview %}\n {% capture orders_json %}\n [\n {\n \"id\": \"gid://shopify/Order/1234567890\",\n \"name\": \"#TEST\",\n \"lineItems\": {\n \"edges\": [\n {\n \"node\": {\n \"customAttributes\": [\n {\n \"key\": {{ include_only_these_property_names[0] | default: \"alpha\" | json }},\n \"value\": \"beta\"\n },\n {\n \"key\": \"gamma\",\n \"value\": \"delta\"\n }\n ]\n }\n },\n {\n \"node\": {\n \"customAttributes\": [\n {\n \"key\": {{ exclude_these_property_names[0] | default: \"zeta\" | json }},\n \"value\": \"epsilon\"\n }\n ]\n }\n }\n ]\n }\n }\n ]\n {% endcapture %}\n\n {% assign orders = orders_json | parse_json %}\n{% endif %}\n\n{% for order in orders %}\n {% assign tags_to_add = array %}\n\n {% assign line_items = order.lineItems.edges | map: \"node\" %}\n\n {% for line_item in line_items %}\n {% if line_item.customAttributes == blank %}\n {% continue %}\n {% endif %}\n\n {% for line_item_property in line_item.customAttributes %}\n {% assign line_item_property_name = line_item_property[\"key\"] %}\n {% assign line_item_property_value = line_item_property[\"value\"] %}\n\n {% if line_item_property_value == blank %}\n {% continue %}\n {% endif %}\n\n {% if include_only_these_property_names != blank %}\n {% unless include_only_these_property_names contains line_item_property_name %}\n {% continue %}\n {% endunless %}\n\n {% elsif exclude_these_property_names != blank %}\n {% if exclude_these_property_names contains line_item_property_name %}\n {% continue %}\n {% endif %}\n {% endif %}\n\n {% assign tag_should_have\n = line_item_property_name\n | append: separator\n | append: line_item_property_value\n %}\n\n {% unless order.tags contains tag_should_have or tag_should_have.size > 40 %}\n {% assign tags_to_add = tags_to_add | push: tag_should_have %}\n {% endunless %}\n {% endfor %}\n {% endfor %}\n\n {% if tags_to_add != blank %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ order.id | json }}\n tags: {{ tags_to_add | uniq | json }}\n ) {\n node {\n ... on Order {\n id\n name\n tags\n }\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n{% endfor %}",
"subscriptions": [
"shopify/orders/create",
"mechanic/user/trigger"
],
"subscriptions_template": "shopify/orders/create\nmechanic/user/trigger",
"tags": [
"Auto-Tag",
"Line Item Properties",
"Orders"
]
}