-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathauto-tag-customers-based-on-discount-codes-used.json
24 lines (24 loc) · 6.79 KB
/
auto-tag-customers-based-on-discount-codes-used.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 easily identify customers who used certain discount codes. This task runs automatically on incoming orders, and can be run manually to tag customers based on historical orders. Optionally, choose to remove tags, instead of adding them.\n\nThis task runs automatically on incoming orders. Run this task manually to scan your store's order history, tagging customers based on historical orders.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-tag customers based on discount codes used",
"online_store_javascript": null,
"options": {
"discount_codes_and_tags__keyval_required": {},
"untag_customers_instead_of_tagging_them__boolean": null,
"allow_partial_matches_when_checking_for_discount_codes__boolean": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% comment %}\n Options order:\n\n {{ options.discount_codes_and_tags__keyval_required }}\n {{ options.untag_customers_instead_of_tagging_them__boolean }}\n{% endcomment %}\n\n{% if options.untag_customers_instead_of_tagging_them__boolean %}\n {% assign tagging_mutation_name = \"tagsRemove\" %}\n{% else %}\n {% assign tagging_mutation_name = \"tagsAdd\" %}\n{% endif %}\n\n{% if event.topic contains \"shopify/orders/\" %}\n {% if event.preview %}\n {% capture order_json %}\n {\n \"discount_applications\": [\n {\n \"type\": \"discount_code\",\n \"code\": {{ options.discount_codes_and_tags__keyval_required.first[0] | json }}\n }\n ],\n \"customer\": {\n \"admin_graphql_api_id\": \"gid://shopify/Customer/1234567890\",\n \"tags\": \"\"\n }\n }\n {% endcapture %}\n\n {% assign order = order_json | parse_json %}\n {% endif %}\n\n {% comment %}\n Remember: only ever one discount code per order\n {% endcomment %}\n {% assign discount_code = order.discount_applications | where: \"type\", \"discount_code\" | map: \"code\" | first %}\n\n {% if discount_code == blank %}\n {% log \"No discount code for this order; nothing to do.\" %}\n {% else %}\n {% assign tags = array %}\n\n {% if options.allow_partial_matches_when_checking_for_discount_codes__boolean %}\n {% for pair in options.discount_codes_and_tags__keyval_required %}\n {% assign some_discount_code_portion = pair[0] %}\n {% assign some_tag = pair[1] %}\n\n {% if discount_code contains some_discount_code_portion %}\n {% assign tags[tags.size] = some_tag %}\n {% endif %}\n {% endfor %}\n {% elsif options.discount_codes_and_tags__keyval_required[discount_code] != blank %}\n {% assign tags[0] = options.discount_codes_and_tags__keyval_required[discount_code] %}\n {% endif %}\n\n {% if tags == empty %}\n {% log message: \"No matching tags found; nothing to do\", discount_code: discount_code %}\n {% else %}\n {% action \"shopify\" %}\n mutation {\n {{ tagging_mutation_name }}(\n id: {{ order.customer.admin_graphql_api_id | json }}\n tags: {{ tags | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n {% endif %}\n{% elsif event.topic == \"mechanic/user/trigger\" %}\n {% assign query_parts = array %}\n {% for keyval in options.discount_codes_and_tags__keyval_required %}\n {% assign query_parts[query_parts.size] = keyval[0] | json | prepend: \"discount_code:\" %}\n {% endfor %}\n {% assign orders_query = query_parts | join: \" OR \" %}\n\n {% log orders_query: orders_query %}\n\n {% assign orders_cursor = nil %}\n {% for n in (0..100) %}\n {% capture query %}\n query {\n orders(\n first: 250\n after: {{ orders_cursor | json }}\n query: {{ orders_query | json }}\n ) {\n pageInfo {\n hasNextPage\n }\n edges {\n cursor\n node {\n discountCode\n customer {\n id\n tags\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 \"orders\": {\n \"edges\": [\n {\n \"node\": {\n \"discountCode\": {{ options.discount_codes_and_tags__keyval_required.first[0] | json }},\n \"customer\": {\n \"id\": \"gid://shopify/Customer/1234567890\",\n \"tags\": []\n }\n }\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% for edge in result.data.orders.edges %}\n {% assign order_node = edge.node %}\n {% assign discount_code = order_node.discountCode %}\n\n {% assign tags = array %}\n\n {% if options.allow_partial_matches_when_checking_for_discount_codes__boolean %}\n {% for pair in options.discount_codes_and_tags__keyval_required %}\n {% assign some_discount_code_portion = pair[0] %}\n {% assign some_tag = pair[1] %}\n\n {% assign discount_code_downcase = discount_code | downcase %}\n {% assign some_discount_code_portion_downcase = some_discount_code_portion | downcase %}\n\n {% if discount_code_downcase contains some_discount_code_portion_downcase %}\n {% unless order_node.customer.tags contains some_tag %}\n {% assign tags[tags.size] = some_tag %}\n {% endunless %}\n {% endif %}\n {% endfor %}\n {% elsif options.discount_codes_and_tags__keyval_required[discount_code] != blank %}\n {% assign tags[0] = options.discount_codes_and_tags__keyval_required[discount_code] %}\n {% endif %}\n\n {% if tags != empty %}\n {% action \"shopify\" %}\n mutation {\n {{ tagging_mutation_name }}(\n id: {{ order_node.customer.id | json }}\n tags: {{ tags | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n {% endfor %}\n\n {% if result.data.orders.pageInfo.hasNextPage %}\n {% assign orders_cursor = 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",
"Customers",
"Discounts"
]
}