forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.liquid
300 lines (260 loc) · 8.91 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
{% 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 options.base_sort_order__required %}
{% error %}
{{ allowed_base_sort_orders | join: ", " | prepend: "Base sort order must be one of: " | json }}
{% enderror %}
{% endunless %}
{% log %}
{{ options.base_sort_order__required | prepend: "Base sort order for this task run: " | json }}
{% endlog %}
{% assign product_sort_order = options.base_sort_order__required %}
{% 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 %}
{% 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 collections = shop.collections %}
{% if event.preview %}
{% capture collections_json %}
[
{
"id": {{ collection_handles_or_ids_to_include.first | default: "1234567890" | json }},
"admin_graphql_api_id": "gid://shopify/Collection/1234567890"
}
]
{% endcapture %}
{% assign collections = collections_json | parse_json %}
{% endif %}
{% for collection in collections %}
{% assign collection_id_string = "" | append: collection.id %}
{% if collection_handles_or_ids_to_include != blank %}
{% unless collection_handles_or_ids_to_include contains collection_id_string
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_id_string
or collection_handles_or_ids_to_exclude contains collection.handle %}
{% continue %}
{% endif %}
{% endif %}
{% if collection.sort_order != "manual" %}
{% if force_manual_sorting_on_collections or event.preview %}
{% action "shopify" %}
mutation {
collectionUpdate(
input: {
id: {{ collection.admin_graphql_api_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 %}
{% assign all_product_ids_current_sort = array %}
{% assign cursor = nil %}
{% for n in (0..100) %}
{% capture query %}
query {
collection(id: {{ collection.admin_graphql_api_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 %}
{% assign in_stock_product_ids = array %}
{% assign out_of_stock_product_ids = array %}
{% assign cursor = nil %}
{% for n in (0..3000) %}
{% capture query %}
query {
collection(id: {{ collection.admin_graphql_api_id | json }}) {
products(
sortKey: {{ product_sort_order }}
reverse: {{ reverse_sort | json }}
first: 9
after: {{ cursor | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
tracksInventory
variants(first: 100) {
nodes {
inventoryPolicy
inventoryQuantity
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"collection": {
"products": {
"nodes": [
{
"id": "gid://shopify/Product/1234567890",
"tracksInventory": true,
"variants": {
"nodes": [
{
"inventoryPolicy": "DENY",
"inventoryQuantity": 0
}
]
}
},
{
"id": "gid://shopify/Product/2345678901",
"tracksInventory": true,
"variants": {
"nodes": [
{
"inventoryPolicy": "CONTINUE",
"inventoryQuantity": 0
}
]
}
},
{
"id": "gid://shopify/Product/3456789012",
"tracksInventory": false
},
{
"id": "gid://shopify/Product/4567890123",
"tracksInventory": true,
"variants": {
"nodes": [
{
"inventoryPolicy": "DENY",
"inventoryQuantity": 0
},
{
"inventoryPolicy": "CONTINUE",
"inventoryQuantity": 0
}
]
}
}
]
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% for product in result.data.collection.products.nodes %}
{% assign has_in_stock_variant = nil %}
{% unless product.tracksInventory %}
{% assign has_in_stock_variant = true %}
{% else %}
{% for variant in product.variants.nodes %}
{% if variant.inventoryPolicy == "CONTINUE" or variant.inventoryQuantity > 0 %}
{% assign has_in_stock_variant = true %}
{% break %}
{% endif %}
{% endfor %}
{% endunless %}
{% if has_in_stock_variant %}
{% assign in_stock_product_ids[in_stock_product_ids.size] = product.id %}
{% else %}
{% assign out_of_stock_product_ids[out_of_stock_product_ids.size] = product.id %}
{% endif %}
{% endfor %}
{% if result.data.collection.products.pageInfo.hasNextPage %}
{% assign cursor = result.data.collection.products.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% assign all_product_ids = in_stock_product_ids | concat: out_of_stock_product_ids %}
{% 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.size] = move %}
{% endif %}
{% endfor %}
{% 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.admin_graphql_api_id | json }}
moves: {{ move_group | graphql_arguments }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% else %}
{% log
message: "No position moves necessary for this collection, everything is already in its appropriate sort order.",
collection: collection.title
%}
{% endfor %}
{% endfor %}