-
Notifications
You must be signed in to change notification settings - Fork 35
/
auto-tag-customers-within-a-purchase-range.json
29 lines (29 loc) · 5.49 KB
/
auto-tag-customers-within-a-purchase-range.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
28
29
{
"docs": "Use this task to auto-tag customers when their historical purchase total falls within a certain range. For example, by configuring this task to evaluate purchase thresholds by number of orders, and by setting the minimum to \"1\" and the maximum to \"12\", this task will auto-tag customers who've made at least 1 purchase, and will *remove* the tag when the 13th purchase is made.\n\nThis task can total up purchases by number of orders, adding line item quantities, or by adding line item subtotals. Optionally, choose a specific product ID to filter by. [Learn how to find a product ID](https://docs.usemechanic.com/article/360-how-do-i-find-an-id-for-a-product-collection-order-or-something-else)\n\nThis task will run whenever a new order is created (or paid, if you choose to only count paid orders), and whenever an order is cancelled.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-tag customers within a purchase range",
"online_store_javascript": null,
"options": {
"purchase_minimum__number_required": "",
"purchase_maximum__number_required": "",
"evaluate_purchase_thresholds_by_number_of_orders__boolean": true,
"evaluate_purchase_thresholds_by_adding_line_item_quantities__boolean": false,
"evaluate_purchase_thresholds_by_adding_line_item_subtotals__boolean": false,
"only_count_paid_orders__boolean": false,
"only_count_this_product_id__number": "",
"customer_tag_to_apply__required": ""
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% assign purchase_minimum = options.purchase_minimum__number_required %}\n{% assign purchase_maximum = options.purchase_maximum__number_required %}\n\n{% if purchase_minimum < 1 or purchase_maximum < 1 %}\n {% error \"Purchase minimum and maximum should both be greater than zero.\" %}\n{% elsif purchase_minimum > purchase_maximum %}\n {% error \"Purchase maximum should be equal to or greater than purchase minimum.\" %}\n{% endif %}\n\n{% assign mode_selections = 0 %}\n{% if options.evaluate_purchase_thresholds_by_number_of_orders__boolean %}\n {% assign mode = \"orders\" %}\n {% assign mode_selections = mode_selections | plus: 1 %}\n{% endif %}\n{% if options.evaluate_purchase_thresholds_by_adding_line_item_quantities__boolean %}\n {% assign mode = \"quantity\" %}\n {% assign mode_selections = mode_selections | plus: 1 %}\n{% endif %}\n{% if options.evaluate_purchase_thresholds_by_adding_line_item_subtotals__boolean %}\n {% assign mode = \"subtotal\" %}\n {% assign mode_selections = mode_selections | plus: 1 %}\n{% endif %}\n\n{% if mode_selections == 0 %}\n {% error \"Choose one method of evaluating the purchase thresholds. :)\" %}\n{% elsif mode_selections > 1 %}\n {% error \"Choose *only* one method of evaluating the purchase thresholds. :)\" %}\n{% endif %}\n\n{% assign purchase_total = 0 %}\n\n{% assign orders_scope = order.customer.orders.any %}\n{% if options.only_count_paid_orders__boolean %}\n {% assign orders_scope = orders_scope.paid %}\n{% endif %}\n\n{% for some_order in orders_scope %}\n {% if some_order.cancelled_at != blank %}\n {% continue %}\n {% endif %}\n\n {% for line_item in some_order.line_items %}\n {% if options.only_count_this_product_id__number != blank and line_item.product_id != options.only_count_this_product_id__number %}\n {% continue %}\n {% endif %}\n\n {% case mode %}\n {% when \"orders\" %}\n {% assign purchase_total = purchase_total | plus: 1 %}\n {% break %}\n {% when \"quantity\" %}\n {% assign purchase_total = purchase_total | plus: line_item.quantity %}\n {% when \"subtotal\" %}\n {% assign purchase_total = line_item.price | times: line_item.quantity | plus: purchase_total %}\n {% endcase %}\n {% endfor %}\n{% endfor %}\n\n{% if event.preview %}\n {% assign purchase_total = purchase_minimum %}\n {% assign customer = '{\"admin_graphql_api_id\":\"gid://shopify/Customer/1234567890\",\"tags\":\"\"}' | parse_json %}\n{% else %}\n {% assign customer = order.customer %}\n{% endif %}\n\n{% log message: \"Purchase total for this customer\", purchase_total: purchase_total %}\n\n{% assign customer_tags_to_match = customer.tags | downcase | split: \", \" %}\n{% assign customer_tag_to_match = options.customer_tag_to_apply__required | downcase %}\n{% if purchase_total >= purchase_minimum and purchase_total <= purchase_maximum %}\n {% unless customer_tags_to_match contains customer_tag_to_match %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ customer.admin_graphql_api_id | json }}\n tags: {{ options.customer_tag_to_apply__required | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endunless %}\n{% else %}\n {% if customer_tags_to_match contains customer_tag_to_match %}\n {% action \"shopify\" %}\n mutation {\n tagsRemove(\n id: {{ customer.admin_graphql_api_id | json }}\n tags: {{ options.customer_tag_to_apply__required | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n{% endif %}",
"subscriptions": [
"shopify/orders/create",
"shopify/orders/cancelled"
],
"subscriptions_template": "{% if options.only_count_paid_orders__boolean %}\n shopify/orders/paid\n{% else %}\n shopify/orders/create\n{% endif %}\n\nshopify/orders/cancelled",
"tags": [
"Auto-Tag",
"Customers",
"Loyalty"
]
}