-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathscript.liquid
529 lines (464 loc) · 16.6 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
{% assign bundle_sku = options.bundle_product_sku__required %}
{% assign component_skus_and_quantities = options.component_product_skus_and_quantities_per_bundle__keyval_number_required %}
{% assign inventory_buffer_quantity = options.inventory_buffer_quantity__number %}
{% assign component_skus = component_skus_and_quantities | keys %}
{% assign all_skus = array | push: bundle_sku | concat: component_skus %}
{% assign variants_by_sku = hash %}
{% assign primary_location = shop.locations[shop.primary_location_id] %}
{% assign primary_location_id_string = shop.primary_location_id | append: "" %}
{% log
task_config: "for this event run",
bundle_sku: bundle_sku,
component_skus_and_quantities: component_skus_and_quantities,
inventory_buffer_quantity: inventory_buffer_quantity,
primary_location: primary_location
%}
{% assign do_full_sync = false %}
{% assign do_component_adjustment = false %}
{% assign component_adjustment_bundle_quantity = nil %}
{% if event.topic == "mechanic/user/trigger" %}
{% assign do_full_sync = true %}
{% endif %}
{% if event.topic == "shopify/orders/create" %}
{% comment %}
-- when new orders are created, only send along bundle line items that are assigned to the primary location
{% endcomment %}
{% capture query %}
query {
order(id: {{ order.admin_graphql_api_id | json }}) {
fulfillmentOrders(
first: 10
query: "assigned_location_id:{{ shop.primary_location_id }}"
) {
nodes {
lineItems(first: 100) {
nodes {
sku
totalQuantity
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"order": {
"fulfillmentOrders": {
"nodes": [
{
"lineItems": {
"nodes": [
{
"sku": {{ bundle_sku | json }},
"totalQuantity": 1
}
]
}
}
]
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign bundle_quantity = 0 %}
{% for fulfillment_order in result.data.order.fulfillmentOrders.nodes %}
{% for line_item in fulfillment_order.lineItems.nodes %}
{% if line_item.sku == bundle_sku %}
{% assign bundle_quantity = bundle_quantity | plus: line_item.totalQuantity %}
{% endif %}
{% endfor %}
{% endfor %}
{% if bundle_quantity != 0 %}
{% assign do_component_adjustment = true %}
{% assign component_adjustment_bundle_quantity = bundle_quantity | times: -1 %}
{% else %}
{% log "None of the fulfillment orders on this order were assigned to the primary location; skipping." %}
{% endif %}
{% endif %}
{% if event.topic == "shopify/refunds/create" %}
{% comment %}
-- when refunds are created, only send along bundle line items that are restocked to the primary location
{% endcomment %}
{% capture query %}
query {
refund(id: {{ refund.admin_graphql_api_id | json }}) {
refundLineItems(first: 100) {
nodes {
lineItem {
sku
}
location {
legacyResourceId
}
quantity
restocked
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"refund": {
"refundLineItems": {
"nodes": [
{
"lineItem": {
"sku": {{ bundle_sku | json }}
},
"location": {
"legacyResourceId": {{ shop.primary_location_id | json }}
},
"quantity": 1,
"restocked": true
}
]
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign bundle_restock_quantity = 0 %}
{% for refund_line_item in result.data.refund.refundLineItems.nodes %}
{% if refund_line_item.lineItem.sku == bundle_sku
and refund_line_item.location.legacyResourceId == primary_location_id_string
and refund_line_item.restocked
%}
{% assign bundle_restock_quantity = bundle_restock_quantity | plus: refund_line_item.quantity %}
{% endif %}
{% endfor %}
{% if bundle_restock_quantity != 0 %}
{% assign do_component_adjustment = true %}
{% assign component_adjustment_bundle_quantity = bundle_restock_quantity %}
{% else %}
{% log "None of the line items on this refund were restocked to the primary location; skipping." %}
{% endif %}
{% endif %}
{% if event.topic contains "shopify/inventory_levels/" %}
{% comment %}
-- only respond to inventory level events at the primary location
{% endcomment %}
{% if inventory_level.location_id != shop.primary_location_id %}
{% log "This inventory level change was for an item not at the primary location; skipping." %}
{% break %}
{% endif %}
{% capture query %}
query {
inventoryLevel(id: {{ inventory_level.admin_graphql_api_id | json }}) {
item {
variant {
sku
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"inventoryLevel": {
"item": {
"variant": {
"sku": {{ component_skus.first | json }}
}
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign updated_sku = result.data.inventoryLevel.item.variant.sku %}
{% if all_skus contains updated_sku %}
{% assign do_full_sync = true %}
{% else %}
{% log "The inventory level change did not include one of the SKUs configured in this task; skipping." %}
{% endif %}
{% endif %}
{% if do_component_adjustment or do_full_sync %}
{% comment %}
-- get available inventory of the bundle and component(s) at the primary location
{% endcomment %}
{% assign query_components = array %}
{% for sku in all_skus %}
{% assign query_components[query_components.size] = sku | json | prepend: "sku:" %}
{% endfor %}
{% for n in (0..500) %}
{% assign cursor = nil %}
{% capture query %}
query {
productVariants(
first: 150
query: {{ query_components | join: " OR " | json }}
after: {{ cursor | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
sku
inventoryItem {
id
inventoryLevel(locationId: {{ primary_location.admin_graphql_api_id | json }}) {
id
quantities(names: "available") {
name
quantity
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"productVariants": {
"nodes": [
{
"sku": {{ bundle_sku | json }},
"inventoryItem": {
"id": "gid://shopify/InventoryItem/1234567890",
"inventoryLevel": {
"id": "gid://shopify/InventoryLevel/1234567890?x=bundle&sku={{ bundle_sku }}&inventory_item_id=1234567890",
"quantities": [
{
"name": "available",
"quantity": 2
}
]
}
}
},
{% for component_sku in component_skus %}
{
"sku": {{ component_sku | json }},
"inventoryItem": {
"id": "gid://shopify/InventoryItem/2345678901",
"inventoryLevel": {
"id": "gid://shopify/InventoryLevel/2345678901?x=component&sku={{ component_sku }}&inventory_item_id=2345678901",
"quantities": [
{
"name": "available",
"quantity": 1
}
]
}
}
}
{% unless forloop.last %},{% endunless %}
{% endfor %}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% for variant in result.data.productVariants.nodes %}
{% assign variants_by_sku[variant.sku]
= variants_by_sku[variant.sku]
| default: array
| push: variant
%}
{% endfor %}
{% if result.data.productVariants.pageInfo.hasNextPage %}
{% assign cursor = result.data.productVariants.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% if variants_by_sku[bundle_sku].size != 1 %}
{% error
message: "Did not find exactly one variant for the provided bundle SKU. This task does not support re-use of the bundle SKU across multiple variants.",
bundle_sku: bundle_sku,
bundle_variants: variants_by_sku[bundle_sku]
%}
{% endif %}
{% for component_sku in component_skus %}
{% if variants_by_sku[component_sku] == blank %}
{% error
message: "Didn't find any variants for a component SKU. Each component SKU must have at least one variant.",
component_sku: component_sku
%}
{% endif %}
{% endfor %}
{% log bundle_and_component_quantities_before_adjustment: variants_by_sku %}
{% endif %}
{% if do_component_adjustment %}
{% comment %}
-- an order was created or a refund with restock occurred which contained the bundle, adjust the inventory of the components
{% endcomment %}
{% assign inventory_adjustments = array %}
{% for keyval in variants_by_sku %}
{% assign sku = keyval[0] %}
{% assign variants = keyval[1] %}
{% assign expected_inventory_quantity = variants[0].inventoryItem.inventoryLevel.quantities.first.quantity %}
{% for variant in variants %}
{% if variant.inventoryItem.inventoryLevel.quantities.first.quantity != expected_inventory_quantity %}
{% error
message: "Expected all variants for a single SKU to have matching inventory levels, but they did not - can't continue. Please manually sync inventory for all variants sharing this SKU, and try again.",
sku: sku,
variants: variants
%}
{% endif %}
{% assign variant_delta = component_adjustment_bundle_quantity | times: component_skus_and_quantities[variant.sku] %}
{% if variant_delta != 0 %}
{% assign inventory_adjustment = hash %}
{% assign inventory_adjustment["inventoryItemId"] = variant.inventoryItem.id %}
{% assign inventory_adjustment["locationId"] = primary_location.admin_graphql_api_id %}
{% assign inventory_adjustment["delta"] = variant_delta %}
{% assign inventory_adjustments = inventory_adjustments | push: inventory_adjustment %}
{% endif %}
{% endfor %}
{% endfor %}
{% if inventory_adjustments != blank %}
{% action "shopify" %}
mutation {
inventoryAdjustQuantities(
input: {
reason: "correction"
name: "available"
changes: {{ inventory_adjustments | graphql_arguments }}
}
) {
inventoryAdjustmentGroup {
reason
changes(quantityNames: ["available"]) {
name
delta
item {
id
sku
}
location {
name
}
}
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% endif %}
{% endif %}
{% if do_full_sync %}
{% comment %}
Initialize some variables:
* minimum_inventory_component_variant: the single component variant that, when its bundle quantity
multiplier is factored in, represents the smallest number of bundles that can be constructed
with the variant inventory on hand. (for example, given 3 widgets per bundle, if we find a widget
variant with an inventory of 5 and another one with inventory 6, the *first* one is the minimum
inventory component variant, because it pulls down the max number of bundles from 2 to 1.)
* minimum_inventory_component_variant_bundle_quantity: the number of bundles that can be safely
constructed, given the smallest proportionate inventory value found across all component variants.
* bundle_variant: the bundle variant (remember, we only support one - no SKU sharing for bundles). we'll
use this variant to perform any inventory updates needed to the bundle itself.
{% endcomment %}
{% assign minimum_inventory_component_variant = nil %}
{% assign minimum_inventory_component_variant_bundle_quantity = nil %}
{% assign bundle_variant = nil %}
{% for pair in variants_by_sku %}
{% assign sku = pair[0] %}
{% assign variants = pair[1] %}
{% assign expected_inventory_quantity = variants[0].inventoryItem.inventoryLevel.quantities.first.quantity %}
{% for variant in variants %}
{% if variant.inventoryItem.inventoryLevel.quantities.first.quantity != expected_inventory_quantity %}
{% assign error_message = "Expected all " | append: sku | append: " variants to have an inventory quantity of " | append: expected_inventory_quantity | append: ", but not every variant is at this level. Ensure every variant is in sync, and try again." %}
{% error error_message %}
{% endif %}
{% if variant.sku == bundle_sku %}
{% assign bundle_variant = variant %}
{% elsif component_skus contains variant.sku %}
{% assign variant_bundle_quantity
= variant.inventoryItem.inventoryLevel.quantities.first.quantity
| times: 1.0
| divided_by: component_skus_and_quantities[variant.sku]
%}
{% if minimum_inventory_component_variant == nil or minimum_inventory_component_variant_bundle_quantity > variant_bundle_quantity %}
{% assign minimum_inventory_component_variant = variant %}
{% assign minimum_inventory_component_variant_bundle_quantity = variant_bundle_quantity %}
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
{% assign bundle_delta
= minimum_inventory_component_variant_bundle_quantity
| floor
| minus: bundle_variant.inventoryItem.inventoryLevel.quantities.first.quantity
%}
{% if inventory_buffer_quantity != blank %}
{% assign bundle_delta = bundle_delta | minus: inventory_buffer_quantity %}
{% endif %}
{% log
bundle_variant: bundle_variant,
minimum_inventory_component_variant_sku: minimum_inventory_component_variant.sku,
minimum_inventory_component_variant_quantity: minimum_inventory_component_variant.inventoryItem.inventoryLevel.quantities.first.quantity, minimum_inventory_component_variant_bundle_quantity: minimum_inventory_component_variant_bundle_quantity,
inventory_buffer_quantity: inventory_buffer_quantity,
available_delta: bundle_delta
%}
{% if bundle_delta == 0 %}
{% log "Lowest quantity component SKU had an inventory that was appropriate for the bundle's current inventory. No change needed." %}
{% else %}
{% action "shopify" %}
mutation {
inventoryAdjustQuantities(
input: {
reason: "correction"
name: "available"
changes: [
{
inventoryItemId: {{ bundle_variant.inventoryItem.id | json }}
locationId: {{ primary_location.admin_graphql_api_id | json }}
delta: {{ bundle_delta }}
}
]
}
) {
inventoryAdjustmentGroup {
reason
changes(quantityNames: ["available"]) {
name
delta
item {
id
sku
}
location {
name
}
}
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% endif %}
{% endif %}