-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathscript.liquid
258 lines (228 loc) · 6.01 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
{% assign tag_to_add = options.tag_to_add__required %}
{% if event.topic contains "shopify/orders/" %}
{% comment %}
-- get order and customer data
{% endcomment %}
{% capture query %}
query {
order(id: {{ order.admin_graphql_api_id | json }}) {
id
customer {
id
tags
}
lineItems(first: 250) {
nodes {
variant {
compareAtPrice
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"order": {
"id": "gid://shopify/Order/1234567890",
"customer": {
"id": "gid://shopify/Customer/1234567890"
},
"lineItems": {
"nodes": [
{
"variant": {
"compareAtPrice": "100.0"
}
}
]
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign order = result.data.order %}
{% assign customer = order.customer %}
{% if customer == blank %}
{% break %}
{% endif %}
{% comment %}
-- if any line item has a variant with a compare at price set, then tag the customer
{% endcomment %}
{% for line_item in order.lineItems.nodes %}
{% if line_item.variant.compareAtPrice != blank %}
{% unless customer.tags contains tag_to_add %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ customer.id | json }}
tags: {{ tag_to_add | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endunless %}
{% break %}
{% endif %}
{% endfor %}
{% elsif event.topic == "mechanic/user/trigger" %}
{% comment %}
-- get all customers who do not yet have the tag and who have placed at least one order
{% endcomment %}
{% assign cursor = nil %}
{% assign customer_ids = array %}
{% for n in (1..100) %}
{% capture query %}
query {
customerSegmentMembers(
first: 1000
after: {{ cursor | json }}
query: "customer_tags NOT CONTAINS '{{ tag_to_add }}' AND number_of_orders > 0"
) {
pageInfo {
hasNextPage
endCursor
}
edges {
node {
id
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"customerSegmentMembers": {
"edges": [
{
"node": {
"id": "gid://shopify/customerSegmentMember/1234567890"
}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% for edge in result.data.customerSegmentMembers.edges %}
{% assign customer_ids[customer_ids.size] = edge.node.id | remove: "SegmentMember" %}
{% endfor %}
{% if result.data.customerSegmentMembers.pageInfo.hasNextPage %}
{% assign cursor = result.data.customerSegmentMembers.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% endif %}
{% for customer_id in customer_ids %}
{% assign customer_qualifies = nil %}
{% comment %}
-- get uncancelled orders data for customer
{% endcomment %}
{% assign cursor = nil %}
{% for n in (1..100) %}
{% capture query %}
query {
customer(id: {{ customer_id | json }}) {
orders(
first: 50
after: {{ cursor | json }}
query: "-status:cancelled"
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
lineItems(first: 250) {
nodes {
variant {
compareAtPrice
}
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"customer": {
"orders": {
"nodes": [
{
"lineItems": {
"nodes": [
{
"variant": {
"compareAtPrice": "100.0"
}
}
]
}
}
]
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% comment %}
-- break out of orders loop as soon as customer qualifies
{% endcomment %}
{% for order in result.data.customer.orders.nodes %}
{% for line_item in order.lineItems.nodes %}
{% if line_item.variant.compareAtPrice != blank %}
{% assign customer_qualifies = true %}
{% break %}
{% endif %}
{% endfor %}
{% if customer_qualifies %}
{% break %}
{% endif %}
{% endfor %}
{% if result.data.customer.orders.pageInfo.hasNextPage and customer_qualifies != true %}
{% assign cursor = result.data.customer.orders.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% if customer_qualifies %}
{% unless customer.tags contains tag_to_add %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ customer_id | json }}
tags: {{ tag_to_add | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endunless %}
{% endif %}
{% endfor %}