-
Notifications
You must be signed in to change notification settings - Fork 35
/
script.liquid
222 lines (194 loc) · 5.61 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
{% assign tag_for_online_orders = options.tag_for_online_orders %}
{% if event.topic contains "shopify/orders" %}
{% capture query %}
query {
order(id: {{ order.admin_graphql_api_id | json }}) {
id
name
customer {
id
tags
}
retailLocation {
name
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"order": {
"id": "gid://shopify/Order/1234567890",
"customer": {
"id": "gid://shopify/Customer/1234567890"
},
"retailLocation": {
"name": "Storefront"
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign order = result.data.order %}
{% assign customer = order.customer %}
{% assign location_name = order.retailLocation.name | default: tag_for_online_orders %}
{% unless customer == blank or location_name == blank or customer.tags contains location_name %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ customer.id | json }}
tags: {{ location_name | json }}
) {
node {
... on Customer {
id
displayName
tags
}
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endunless %}
{% elsif event.topic == "mechanic/user/trigger" %}
{% comment %}
-- get IDs of all customers who have placed an order
{% endcomment %}
{% assign cursor = nil %}
{% assign customer_ids = array %}
{% for n in (1..100) %}
{% capture query %}
query {
customerSegmentMembers(
first: 1000
after: {{ cursor | json }}
query: "number_of_orders > 0"
) {
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_have_placed_an_order: customer_ids.size %}
{% endunless %}
{% if event.preview %}
{% assign customer_ids[0] = "gid://shopify/Customer/1234567890" %}
{% endif %}
{% for customer_id in customer_ids %}
{% comment %}
-- get all relevant order data for this customer
{% endcomment %}
{% assign cursor = nil %}
{% assign tags_to_add = array %}
{% for n in (1..10) %}
{% capture query %}
query {
customer(id: {{ customer_id | json }}) {
id
tags
orders(
first: 250
after: {{ cursor | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
name
retailLocation {
name
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"customer": {
"id": "gid://shopify/Customer/1234567890",
"orders": {
"nodes": [
{
"id": "gid://shopify/Order/1234567890",
"retailLocation": {
"name": "Storefront"
}
}
]
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign customer = result.data.customer %}
{% comment %}
-- process orders to see which location names should be set as tags
{% endcomment %}
{% for order in customer.orders.nodes %}
{% assign location_name = order.retailLocation.name | default: tag_for_online_orders %}
{% unless location_name == blank or customer.tags contains location_name or tags_to_add contains location_name %}
{% assign tags_to_add = tags_to_add | push: location_name %}
{% endunless %}
{% endfor %}
{% if result.data.customer.orders.pageInfo.hasNextPage %}
{% assign cursor = result.data.customer.orders.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% if tags_to_add != blank %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ customer.id | json }}
tags: {{ tags_to_add | sort_natural | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endfor %}
{% endif %}