-
Notifications
You must be signed in to change notification settings - Fork 35
/
script.liquid
413 lines (353 loc) · 11.9 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
{% assign base_sort_order = options.base_sort_order__required %}
{% assign collection_handles_or_ids_to_include = options.collection_handles_or_ids_to_include__array %}
{% assign collection_handles_or_ids_to_exclude = options.collection_handles_or_ids_to_exclude__array %}
{% assign force_manual_sorting_on_collections = options.force_manual_sorting_on_collections__boolean %}
{% assign allowed_base_sort_orders = "MANUAL,BEST_SELLING,ALPHA_ASC,ALPHA_DESC,PRICE_DESC,PRICE_ASC,CREATED_DESC,CREATED" | split: "," %}
{% unless allowed_base_sort_orders contains base_sort_order %}
{% error %}
{{ allowed_base_sort_orders | join: ", " | prepend: "Base sort order must be one of: " | json }}
{% enderror %}
{% endunless %}
{% log %}
{{ base_sort_order | prepend: "Base sort order for this task run: " | json }}
{% endlog %}
{% assign product_sort_order = base_sort_order %}
{% assign reverse_sort = nil %}
{% case product_sort_order %}
{% when "ALPHA_ASC" %}
{% assign product_sort_order = "TITLE" %}
{% when "ALPHA_DESC" %}
{% assign product_sort_order = "TITLE" %}
{% assign reverse_sort = true %}
{% when "CREATED_DESC" %}
{% assign product_sort_order = "CREATED" %}
{% assign reverse_sort = true %}
{% when "PRICE_ASC" %}
{% assign product_sort_order = "PRICE" %}
{% when "PRICE_DESC" %}
{% assign product_sort_order = "PRICE" %}
{% assign reverse_sort = true %}
{% endcase %}
{% comment %}
-- query all collections in the shop; do not use a query filter for IDs or handles here since those configuration fields are optional
{% endcomment %}
{% assign cursor = nil %}
{% assign collections = array %}
{% for n in (1..100) %}
{% capture query %}
query {
collections(
first: 250
after: {{ cursor | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
legacyResourceId
title
handle
sortOrder
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"collections": {
"nodes": [
{
"id": "gid://shopify/Collection/1234567890",
"legacyResourceId": {{ collection_handles_or_ids_to_include.first | default: "1234567890" | json }},
"title": "Samples",
"handle": {{ collection_handles_or_ids_to_include.first | json }},
"sortOrder": "MANUAL"
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign collections = collections | concat: result.data.collections.nodes %}
{% if result.data.collections.pageInfo.hasNextPage %}
{% assign cursor = result.data.collections.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% comment %}
-- loop through collections and filter by ID and/or handle as configured
{% endcomment %}
{% for collection in collections %}
{% if collection_handles_or_ids_to_include != blank %}
{% unless collection_handles_or_ids_to_include contains collection.legacyResourceId
or collection_handles_or_ids_to_include contains collection.handle %}
{% continue %}
{% endunless %}
{% elsif collection_handles_or_ids_to_exclude != blank %}
{% if collection_handles_or_ids_to_exclude contains collection.legacyResourceId
or collection_handles_or_ids_to_exclude contains collection.handle %}
{% continue %}
{% endif %}
{% endif %}
{% comment %}
-- make sure collection is configured for manual sorting, and optionally update it if not
{% endcomment %}
{% if collection.sortOrder != "MANUAL" %}
{% if force_manual_sorting_on_collections %}
{% action "shopify" %}
mutation {
collectionUpdate(
input: {
id: {{ collection.id | json }}
sortOrder: MANUAL
}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% else %}
{% log %}
{{ collection.title | json | append: " is not configured for manual sorting; skipping." | json }}
{% endlog %}
{% continue %}
{% endif %}
{% endif %}
{% comment %}
-- get all product IDs in this collection using the current sort order
{% endcomment %}
{% assign all_product_ids_current_sort = array %}
{% assign cursor = nil %}
{% for n in (1..100) %}
{% capture query %}
query {
collection(id: {{ collection.id | json }}) {
products(
sortKey: COLLECTION_DEFAULT
first: 250
after: {{ cursor | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% assign product_ids_batch = result.data.collection.products.nodes | map: "id" %}
{% assign all_product_ids_current_sort = all_product_ids_current_sort | concat: product_ids_batch %}
{% if result.data.collection.products.pageInfo.hasNextPage %}
{% assign cursor = result.data.collection.products.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% comment %}
-- get all products in this collection using the configured sort order; get variants later if needed in order to support 2K variant limit
{% endcomment %}
{% assign in_stock_product_ids = array %}
{% assign out_of_stock_product_ids = array %}
{% assign cursor = nil %}
{% for n in (1..100) %}
{% capture query %}
query {
collection(id: {{ collection.id | json }}) {
products(
sortKey: {{ product_sort_order }}
reverse: {{ reverse_sort | json }}
first: 250
after: {{ cursor | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
tracksInventory
hasOutOfStockVariants
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"collection": {
"products": {
"nodes": [
{
"id": "gid://shopify/Product/1234567890",
"tracksInventory": true,
"hasOutOfStockVariants": true
},
{
"id": "gid://shopify/Product/2345678901",
"tracksInventory": true,
"hasOutOfStockVariants": false
},
{
"id": "gid://shopify/Product/3456789012",
"tracksInventory": false
}
]
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% comment %}
-- split products into in-stock and out-of-stock buckets, keeping the configured sort order
{% endcomment %}
{% for product in result.data.collection.products.nodes %}
{% assign has_in_stock_variant = nil %}
{% unless product.tracksInventory and product.hasOutOfStockVariants %}
{% assign has_in_stock_variant = true %}
{% else %}
{% comment %}
-- query up to 2K variants to see if any of them are in stock; can break as soon as one is found
{% endcomment %}
{% assign variants_cursor = nil %}
{% for n in (1..8) %}
{% capture query %}
query {
product(id: {{ product.id | json }}) {
variants(
first: 250
after: {{ variants_cursor | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
inventoryPolicy
inventoryQuantity
}
}
}
}
{% endcapture %}
{% assign variants_result = query | shopify %}
{% if event.preview %}
{% capture variants_result_json %}
{
"data": {
"product": {
"variants": {
"nodes": [
{
"id": "gid://shopify/ProductVariant/1234567890",
"inventoryPolicy": "DENY",
"inventoryQuantity": 0
}
]
}
}
}
}
{% endcapture %}
{% assign variants_result = variants_result_json | parse_json %}
{% endif %}
{% for variant in variants_result.data.product.variants.nodes %}
{% if variant.inventoryPolicy == "CONTINUE" or variant.inventoryQuantity > 0 %}
{% assign has_in_stock_variant = true %}
{% break %}
{% endif %}
{% endfor %}
{% if variants_result.data.product.variants.pageInfo.hasNextPage and has_in_stock_variant != true %}
{% assign variants_cursor = variants_result.data.product.variants.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% endunless %}
{% if has_in_stock_variant %}
{% assign in_stock_product_ids = in_stock_product_ids | push: product.id %}
{% else %}
{% assign out_of_stock_product_ids = out_of_stock_product_ids | push: product.id %}
{% endif %}
{% endfor %}
{% if result.data.collection.products.pageInfo.hasNextPage %}
{% assign cursor = result.data.collection.products.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% comment %}
-- combine product IDs back into single array with all out of stock variants following the in stock ones, but otherwise keeping the configured sort order
{% endcomment %}
{% assign all_product_ids = in_stock_product_ids | concat: out_of_stock_product_ids %}
{% comment %}
-- determine which product IDs need to be moved by comparing to original sort order
{% endcomment %}
{% assign moves = array %}
{% for product_id in all_product_ids %}
{% if all_product_ids_current_sort[forloop.index0] != product_id %}
{% assign move = hash %}
{% assign move["id"] = product_id %}
{% assign move["newPosition"] = "" | append: forloop.index0 %}
{% assign moves = moves | push: move %}
{% endif %}
{% endfor %}
{% comment %}
-- move to next collection if no moves are necessary
{% endcomment %}
{% if moves == blank %}
{% log
message: "No position moves necessary for this collection, everything is already in its appropriate sort order.",
collection: collection.title
%}
{% continue %}
{% endif %}
{% log
message: "Scheduling job(s) to reorder products for this collection.",
collection: collection.title,
moves_count: moves.size
%}
{% comment %}
-- using reverse filter below due to a bug in the collectionReorderProducts mutation
-- this filter will NOT affect the sort order determined above
{% endcomment %}
{% assign move_groups = moves | reverse | in_groups_of: 250, fill_with: false %}
{% for move_group in move_groups %}
{% action "shopify" %}
mutation {
collectionReorderProducts(
id: {{ collection.id | json }}
moves: {{ move_group | graphql_arguments }}
) {
job {
id
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endfor %}
{% endfor %}