forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-tag-customers-who-purchase-an-item-on-sale.json
22 lines (22 loc) · 3.84 KB
/
auto-tag-customers-who-purchase-an-item-on-sale.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"docs": "When a customer orders an item on sale (technically, when the order contains a product variant that includes a \"compare at\" price), this task automatically adds a tag to their account. Useful for keeping track of customers who take advantage of sale pricing!\n\nThis task will run for each new order that's created, tagging customers who purchase a product that has a \"compare at\" price.\r\n\r\nRun this task manually to have Mechanic scan your entire customer base, and each customer's order history. **This may take some time!** To ensure that Mechanic can access your complete history, [make sure \"Read all orders\" is enabled](https://help.usemechanic.com/tutorials/enabling-read_all_orders).\r\n\r\n**Please note**: This task will only tag customers who've purchased a product that _currently_ has a \"compare at\" price. Historical information about \"compare at\" pricing is not available, and so this task cannot make tagging decisions based on historical pricing.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-tag customers who purchase an item on sale",
"online_store_javascript": null,
"options": {
"tag_to_add__required": "discount-shopper"
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% if event.preview %}\n {\n \"action\": {\n \"type\": \"shopify\",\n \"options\": [\n \"update\",\n [\n \"customer\",\n 1234567890\n ],\n {\n \"tags\": {{ options.tag_to_add__required | json }}\n }\n ]\n }\n }\n{% elsif event.topic contains \"shopify/orders/\" %}\n {% assign customer = order.customer.reload %}\n\n {% assign purchase_includes_compare_at_pricing = false %}\n\n {% for line_item in order.line_items %}\n {% if line_item.variant.compare_at_price != blank %}\n {% assign purchase_includes_compare_at_pricing = true %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% if purchase_includes_compare_at_pricing %}\n {% assign customer_tags_to_save = customer.tags | add_tag: options.tag_to_add__required %}\n\n {% if customer.tags != customer_tags_to_save %}\n {\n \"action\": {\n \"type\": \"shopify\",\n \"options\": [\n \"update\",\n [\n \"customer\",\n {{ customer.id | json }}\n ],\n {\n \"tags\": {{ customer_tags_to_save | json }}\n }\n ]\n }\n }\n {% endif %}\n {% endif %}\n{% elsif event.topic == \"mechanic/user/trigger\" %}\n {% for customer in shop.customers %}\n {% assign customer_tags_to_save = customer.tags %}\n {% assign purchase_includes_compare_at_pricing = false %}\n\n {% for order in customer.orders.any %}\n {% for line_item in order.line_items %}\n {% if line_item.variant.compare_at_price != blank %}\n {% assign purchase_includes_compare_at_pricing = true %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% if purchase_includes_compare_at_pricing %}\n {% assign customer_tags_to_save = customer.tags | add_tag: options.tag_to_add__required %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% if customer_tags_to_save != customer.tags %}\n {\n \"action\": {\n \"type\": \"shopify\",\n \"options\": [\n \"update\",\n [\n \"customer\",\n {{ customer.id | json }}\n ],\n {\n \"tags\": {{ customer_tags_to_save | json }}\n }\n ]\n }\n }\n {% endif %}\n {% endfor %}\n{% endif %}",
"subscriptions": [
"shopify/orders/create",
"mechanic/user/trigger"
],
"subscriptions_template": "shopify/orders/create\r\nmechanic/user/trigger",
"tags": [
"Auto-Tag",
"Customers",
"Products"
]
}