-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathauto-tag-new-orders-with-company-metafield-values.json
25 lines (25 loc) · 6.7 KB
/
auto-tag-new-orders-with-company-metafield-values.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": "When new orders are created, this task will check to see if any of the configured metafields are set on the company which placed the order, and if so then it will add order tags based on the metafield values.\n\nConfigure this task by adding company metafields on the left, in the form of *namespace.key*, and the optional paired tag prefixes on the right. Prefixes should include a divider and/or spacing as needed (e.g. \"Batch: \", \"attn_\").\n\nThis task supports the following metafield types - \"boolean\", \"date\", \"date_time\", \"number_decimal\", \"number_integer\", and \"single_line_text_field\". List versions of each type are also supported (e.g. \"list.single_line_text_field\"). List metafields with multiple values set on the company will result in an order tag being added for each value.\n\n**IMPORTANT:** Shopify limits order tags to 40 characters. If a tag (including the optional prefix) exceeds that length, then that tag will not be set on the order. Enable the \"Truncate long tags\" option to have the task truncate tags to a maximum of 40 characters. Truncations are made from the right side of the tag.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-tag new orders with company metafield values",
"online_store_javascript": null,
"options": {
"metafields_and_tag_prefixes__keyval_required": null,
"truncate_long_tags__boolean": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"preview_event_definitions": [],
"script": "{% assign metafields_and_tag_prefixes = options.metafields_and_tag_prefixes__keyval_required %}\n{% assign truncate_long_tags = options.truncate_long_tags__boolean %}\n\n{% assign metafields = metafields_and_tag_prefixes | keys %}\n{% assign supported_metafield_types\n = array\n | push:\n \"boolean\",\n \"date\",\n \"date_time\",\n \"number_decimal\",\n \"number_integer\",\n \"single_line_text_field\"\n%}\n\n{% if event.topic == \"shopify/orders/create\" or event.topic == \"mechanic/user/order\" %}\n {% capture query %}\n query {\n order(id: {{ order.admin_graphql_api_id | json }}) {\n id\n name\n tags\n purchasingEntity {\n __typename\n ... on PurchasingCompany {\n company {\n metafields(\n first: 250\n keys: {{ metafields | graphql_arguments }}\n ) {\n nodes {\n key\n type\n value\n }\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 \"name\": \"#PREVIEW\",\n \"purchasingEntity\": {\n \"__typename\": \"PurchasingCompany\",\n \"company\": {\n \"metafields\": {\n \"nodes\": [\n {\n \"key\": {{ metafields.first | json }},\n \"type\": \"list.single_line_text_field\",\n \"value\": \"[\\\"Preview value\\\"]\"\n }\n ]\n }\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign order = result.data.order %}\n\n {% if order.purchasingEntity.__typename != \"PurchasingCompany\" %}\n {% log \"This order is not a company purchase; skipping.\" %}\n {% break %}\n {% endif %}\n\n {% comment %}\n -- loop through any of the configured metafields that were found on the company for this order\n {% endcomment %}\n\n {% assign tags_to_add = array %}\n\n {% for metafield in order.purchasingEntity.company.metafields.nodes %}\n {% assign tag_prefix = metafields_and_tag_prefixes[metafield.key] %}\n {% assign metafield_type = metafield.type | remove: \"list.\" %}\n\n {% unless supported_metafield_types contains metafield_type %}\n {% log\n message: \"Unsupported metafield type for this task.\",\n metafield: metafield,\n supported_metafield_types: supported_metafield_types\n %}\n {% continue %}\n {% endunless %}\n\n {% comment %}\n -- convert all values to an array so list metafield types can more easily be supported\n {% endcomment %}\n\n {% if metafield.type contains \"list.\" %}\n {% assign metafield_values = metafield.value | parse_json %}\n {% else %}\n {% assign metafield_values = array | push: metafield.value %}\n {% endif %}\n\n {% comment %}\n -- determine tags by metafield value, if there is a prefix tag configured, and length\n {% endcomment %}\n\n {% for metafield_value in metafield_values %}\n {% assign tag = metafield_value | prepend: tag_prefix %}\n\n {% if tag.size > 40 %}\n {% comment %}\n -- order tags only support 40 characters max\n {% endcomment %}\n\n {% if truncate_long_tags %}\n {% assign truncated_tag = tag | slice: 0, 40 %}\n\n {% log\n message: \"Tag is too long to be set on the order, and tag truncation is enabled; applying to this tag.\",\n original_tag: tag,\n truncated_tag: truncated_tag\n %}\n\n {% assign tag = truncated_tag %}\n\n {% else %}\n {% log\n message: \"Tag is too long to be set on the order, and tag truncation is not enabled; skipping this tag,\",\n tag: tag,\n tag_length: tag.size\n %}\n {% continue %}\n {% endif %}\n {% endif %}\n\n {% unless order.tags contains tag %}\n {% assign tags_to_add = tags_to_add | push: tag %}\n {% endunless %}\n {% endfor %}\n {% endfor %}\n\n {% comment %}\n -- add tags to the order as needed\n {% endcomment %}\n\n {% if tags_to_add != blank %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ order.id | json }}\n tags: {{ tags_to_add | json }}\n ) {\n node {\n ... on Order {\n name\n tags\n }\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n\n {% else %}\n {% log \"No tagging operations needed for this order.\" %}\n {% endif %}\n{% endif %}\n",
"subscriptions": [
"shopify/orders/create",
"mechanic/user/order"
],
"subscriptions_template": "shopify/orders/create\nmechanic/user/order",
"tags": [
"Auto-Tag",
"Companies",
"Metafields",
"Orders"
]
}