forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.liquid
66 lines (60 loc) · 2.35 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
{% if event.preview %}
{
"action": {
"type": "shopify",
"options": [
"post",
"/admin/inventory_levels/set.json",
{
"inventory_item_id": 1234567890,
"location_id": {{ shop.primary_location_id | json }},
"available": {{ options.inventory_level_to_apply__number_required | round | json }}
}
]
}
}
{% else %}
{% assign collection_id = options.collection_id__number_required | round %}
{% assign collection = shop.collections[collection_id] %}
{% unless collection %}
{% capture message %}No collection found with ID #{{ collection_id | json }}.{% endcapture %}
{"log": {{ message | json }}}
{% endunless %}
{% if collection.products.count == 0 %}
{% capture message %}No products in collection #{{ collection_id | json }}.{% endcapture %}
{"log": {{ message | json }}}
{% endif %}
{% for product in collection.products %}
{% for variant in product.variants %}
{% if variant.inventory_management != "shopify" %}
{% capture message %}Inventory management policy is {{ variant.inventory_management | json }}, skipping inventory update for {{ variant.title }} on {{ product.title }}{% endcapture %}
{"log": {{ message | json }}}
{% continue %}
{% endif %}
{% for inventory_level in variant.inventory_levels %}
{% if inventory_level.location_id != shop.primary_location_id %}
{% continue %}
{% endif %}
{% if inventory_level.available == options.inventory_level_to_apply__number_required %}
{% capture message %}Inventory level is already {{ inventory_level.available | json }}, skipping inventory update for {{ variant.title }} on {{ product.title }}{% endcapture %}
{"log": {{ message | json }}}
{% continue %}
{% endif %}
{
"action": {
"type": "shopify",
"options": [
"post",
"/admin/inventory_levels/set.json",
{
"inventory_item_id": {{ inventory_level.inventory_item_id | json }},
"location_id": {{ inventory_level.location_id | json }},
"available": {{ options.inventory_level_to_apply__number_required | round | json }}
}
]
}
}
{% endfor %}
{% endfor %}
{% endfor %}
{% endif %}