-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathscript.liquid
193 lines (162 loc) · 6.21 KB
/
script.liquid
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
{% assign required_customer_tag = options.required_customer_tag__required %}
{% assign interval_days = options.include_customers_who_have_not_placed_an_order_in_this_many_days__number_required %}
{% assign email_subject_prefix = options.email_subject_prefix__required %}
{% assign email_recipient = options.email_recipient__required_email %}
{% assign send_email_anyway_if_no_customers_are_found = options.send_email_anyway_if_no_customers_are_found__boolean %}
{% if interval_days <= 0 %}
{% error "The number of days must be greater than 0. :)" %}
{% endif %}
{% assign now_s = "now" | date: "%s" | times: 1 %}
{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
{% comment %}
-- get IDs of all customers with the required tag and who have placed an order over X days
{% endcomment %}
{%- capture customer_segment_query -%}
customer_tags CONTAINS '{{ required_customer_tag }}' AND orders_placed(until: -{{ interval_days }}d) = true
{%- endcapture -%}
{% assign cursor = nil %}
{% assign customer_ids = array %}
{% for n in (1..100) %}
{% capture query %}
query {
customerSegmentMembers(
first: 1000
after: {{ cursor | json }}
query: {{ customer_segment_query | json }}
) {
pageInfo {
hasNextPage
endCursor
}
edges {
node {
id
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% assign customer_segment_members = result.data.customerSegmentMembers.edges | map: "node" %}
{% comment %}
-- remove the "SegmentMember" portion from IDs for easier use in querying each customer for additional data not available in the segment resource
{% endcomment %}
{% for customer_segment_member in customer_segment_members %}
{% assign customer_ids[customer_ids.size] = customer_segment_member.id | remove: "SegmentMember" %}
{% endfor %}
{% if result.data.customerSegmentMembers.pageInfo.hasNextPage %}
{% assign cursor = result.data.customerSegmentMembers.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% unless event.preview %}
{% log count_of_customers_who_qualify: customer_ids.size %}
{% endunless %}
{% if event.preview %}
{% assign customer_ids[0] = "gid://shopify/Customer/1234567890" %}
{% endif %}
{% comment %}
-- get additional customer and last order data and save list item in an array for email body processing
{% endcomment %}
{% assign list_items = array %}
{% for customer_id in customer_ids %}
{% capture query %}
query {
customer(id: {{ customer_id | json }}) {
legacyResourceId
displayName
email
lastOrder {
legacyResourceId
name
processedAt
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"customer": {
"legacyResourceId": "1234567890",
"displayName": "Alpha Beta",
"email": "[email protected]",
"lastOrder": {
"processedAt": {{ interval_days | times: -86400 | plus: now_s | minus: 432000 | date: "%Y-%m-%dT%H:%M:%SZ" | json }},
"legacyResourceId": "1234567890",
"name": "#PREVIEW"
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign customer = result.data.customer %}
{% assign lastOrder_days_ago
= customer.lastOrder.processedAt
| date: "%s"
| minus: now_s
| divided_by: -86400
%}
{% capture list_item %}
<li>
<a href="{{ shop.admin_url }}customers/{{ customer.legacyResourceId }}">{{ customer.displayName | default: "(unnamed)" }}</a>
({{ customer.email | default: "(no email)" }})
<br>
Last ordered on {{ customer.lastOrder.processedAt | date: "%Y-%m-%d" }},
{{ lastOrder_days_ago }} {{ lastOrder_days_ago | pluralize: "day", "days" }} ago
– <a href="{{ shop.admin_url }}orders/{{ customer.lastOrder.legacyResourceId }}">{{ customer.lastOrder.name }}</a>
</li>
{% endcapture %}
{% assign list_items = list_items | push: list_item %}
{% endfor %}
{% comment %}
-- generate the email subject and body with the list item data from qualifying customers
{% endcomment %}
{% capture email_subject %}
{{ email_subject_prefix }} {{ list_items.size }} {{ list_items.size | pluralize: "customer", "customers" }} found
{% endcapture %}
{% if list_items != blank %}
{% capture email_body %}
<p>Hello,</p>
<p>
Filtering for customers tagged {{ required_customer_tag | json }},
the following {{ list_items.size }} {{ list_items.size | pluralize: "customer was", "customers were" }}
found to have not placed an order in the last {{ interval_days }} {{ interval_days | pluralize: "day", "days" }}:
</p>
<ul>
{{ list_items | join: newline }}
</ul>
<p>Thanks,<br>- Mechanic, for {{ shop.name }}</p>
{% endcapture %}
{% action "email" %}
{
"to": {{ email_recipient | json }},
"subject": {{ email_subject | strip | json }},
"body": {{ email_body | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}
{% elsif send_email_anyway_if_no_customers_are_found %}
{% capture email_body %}
Hello,
We didn't find any customers (tagged {{ required_customer_tag | json }}) having zero orders placed in the last {{ interval_days }} day(s).
Thanks,
- Mechanic, for {{ shop.name }}
{% endcapture %}
{% action "email" %}
{
"to": {{ email_recipient | json }},
"subject": {{ email_subject | strip | json }},
"body": {{ email_body | unindent | strip | newline_to_br | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}
{% endif %}
{% endif %}