forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-add-an-order-note-for-returning-customers.json
22 lines (22 loc) · 4.38 KB
/
auto-add-an-order-note-for-returning-customers.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"docs": "Use this task to identify orders that are for customers who have previously ordered from your store. Optionally, ignore more frequent purchasers, by ignoring customers who've made an order recently. Useful for making sure to include a welcome-back gift in order shipments. :)",
"halt_action_run_sequence_on_error": false,
"name": "Auto-add an order note for returning customers",
"online_store_javascript": null,
"options": {
"ignore_customers_who_previously_ordered_after_this_date": null,
"ignore_customers_who_previously_ordered_less_than_x_days_ago__number": 60,
"order_note_to_add__required_multiline": "Add the prodigal customer insert"
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% if options.ignore_customers_who_previously_ordered_after_this_date != blank and options.ignore_customers_who_previously_ordered_less_than_x_days_ago__number != blank %}\n {% error 'Choose only one \"ignore\" option at a time. Thanks! :)' %}\n{% endif %}\n\n{% capture query %}\n query {\n customer(id: {{ order.customer.admin_graphql_api_id | json }}) {\n orders(first: 2, sortKey: CREATED_AT, reverse: true) {\n edges {\n node {\n id\n createdAt\n name\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 \"customer\": {\n \"orders\": {\n \"edges\": [\n {\n \"node\": {\n \"id\": \"gid://shopify/Order/2345678901\",\n \"createdAt\": \"2020-00-00T00:00:00Z\",\n \"name\": \"#1235\"\n }\n },\n {\n \"node\": {\n \"id\": \"gid://shopify/Order/1234567890\",\n \"createdAt\": \"1900-00-00T00:00:00Z\",\n \"name\": \"#1234\"\n }\n }\n ]\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n{% endif %}\n\n{% assign previous_order_node = result.data.customer.orders.edges[1].node %}\n\n{% log previous_order: previous_order_node %}\n\n{% if options.ignore_customers_who_previously_ordered_after_this_date != blank %}\n {% assign nonsense_s = \"asdf\" | date: \"%s\" | times: 1 %}\n {% assign previous_order_threshold_s = options.ignore_customers_who_previously_ordered_after_this_date | date: \"%s\" | times: 1 %}\n {% if previous_order_threshold_s == nonsense_s %}\n {% error %}{{ options.ignore_customers_who_previously_ordered_after_this_date | json | append: \" was not recognized as a valid date\" | json }}{% enderror %}\n {% endif %}\n{% elsif options.ignore_customers_who_previously_ordered_less_than_x_days_ago__number != blank %}\n {% assign time_window_s = options.ignore_customers_who_previously_ordered_less_than_x_days_ago__number | times: 24 | times: 60 | times: 60 %}\n {% assign previous_order_threshold_s = \"now\" | date: \"%s\" | minus: time_window_s %}\n{% endif %}\n\n{% if previous_order_threshold_s %}\n {% log %}{\"previous_order_threshold\": {{ previous_order_threshold_s | date: \"%Y-%m-%d %T %Z\" | json }}}{% endlog %}\n{% endif %}\n\n{% assign previous_order_qualifies = false %}\n{% if previous_order_threshold_s == nil %}\n {% assign previous_order_qualifies = true %}\n{% else %}\n {% if previous_order_node %}\n {% assign previous_order_created_at_s = previous_order_node.createdAt | date: \"%s\" | times: 1 %}\n {% if previous_order_created_at_s < previous_order_threshold_s %}\n {% assign previous_order_qualifies = true %}\n {% endif %}\n {% endif %}\n{% endif %}\n\n{% if previous_order_created_at_s < previous_order_threshold_s %}\n {% assign note = order.note | append: newline | append: newline | append: options.order_note_to_add__required_multiline | strip %}\n\n {% action \"shopify\" %}\n mutation {\n orderUpdate(\n input: {\n id: {{ order.admin_graphql_api_id | json }}\n note: {{ note | json }}\n }\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n{% endif %}",
"subscriptions": [
"shopify/orders/create"
],
"subscriptions_template": "shopify/orders/create",
"tags": [
"Customers",
"Order Note"
]
}