forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.liquid
177 lines (166 loc) · 5.49 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
{% assign out_of_stock_list_items = array %}
{% assign report_csv = array %}
{% assign report_csv[0] = array %}
{% assign report_csv[0][0] = "Display name" %}
{% assign report_csv[0][1] = "Product ID" %}
{% assign report_csv[0][2] = "Variant ID" %}
{% assign report_csv[0][3] = "Location ID" %}
{% assign cursor = nil %}
{% for n in (0..30000) %}
{% capture query %}
query {
productVariants(
first: 3
after: {{ cursor | json }}
sortKey: FULL_TITLE
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
displayName
legacyResourceId
product {
legacyResourceId
}
inventoryItem {
tracked
inventoryLevels(first: 100) {
nodes {
location {
name
legacyResourceId
}
quantities(names: "available") {
name
quantity
}
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"productVariants": {
"nodes": [
{
"displayName": "Short Sleeve T-Shirt - M, Red",
"inventoryItem": {
"tracked": true,
"inventoryLevels": {
"nodes": [
{
"location": {
"name": "Warehouse"
},
"quantities": [
{
"name": "available",
"quantity": 0
}
]
}
]
}
}
},
{
"displayName": "Short Sleeve T-Shirt - S, Blue",
"inventoryItem": {
"tracked": true,
"inventoryLevels": {
"nodes": [
{
"location": {
"name": "Warehouse"
},
"quantities": [
{
"name": "available",
"quantity": -1
}
]
}
]
}
}
},
{
"displayName": "Short Sleeve T-Shirt - XL, Green",
"inventoryItem": {
"tracked": true,
"inventoryLevels": {
"nodes": [
{
"location": {
"name": "Storefront"
},
"quantities": [
{
"name": "available",
"quantity": -5
}
]
}
]
}
}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% for variant in result.data.productVariants.nodes %}
{% if variant.inventoryItem.tracked == false %}
{% continue %}
{% endif %}
{% for inventory_level in variant.inventoryItem.inventoryLevels.nodes %}
{% if inventory_level.quantities.first.quantity <= options.out_of_stock_quantity__number_required %}
{% capture list_item %}
<li>
{% if event.preview %}
<a>{{ variant.displayName }}</a>
{% else %}
<a href="https://{{ shop.domain }}/admin/products/{{ variant.product.legacyResourceId }}/variants/{{ variant.legacyResourceId }}">{{ variant.displayName }}</a>
{% endif %}
({{ inventory_level.quantities.first.quantity }} in {{ inventory_level.location.name }})
</li>
{% endcapture %}
{% assign out_of_stock_list_items[out_of_stock_list_items.size] = list_item %}
{% assign report_csv_row = array %}
{% assign report_csv_row[0] = variant.displayName %}
{% assign report_csv_row[1] = variant.product.legacyResourceId %}
{% assign report_csv_row[2] = variant.legacyResourceId %}
{% assign report_csv_row[3] = inventory_level.location.legacyResourceId %}
{% assign report_csv[report_csv.size] = report_csv_row %}
{% endif %}
{% endfor %}
{% endfor %}
{% if result.data.productVariants.pageInfo.hasNextPage %}
{% assign cursor = result.data.productVariants.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% if out_of_stock_list_items != empty %}
{% action "email" %}
{
"to": {{ options.report_recipient_email__email_required | json }},
"subject": {{ options.report_subject__required | json }},
"body": {{ out_of_stock_list_items | join: newline | prepend: "<p>Hello,</p><p>The following variants are out of stock, in the named locations:</p><ul>" | append: "</ul><p>Thanks,<br>Mechanic, for " | append: shop.name | append: "</p>" | json }},
"attachments": {
{{ "now" | date: "%Y%m%d" | prepend: "out-of-stock-" | append: ".csv" | json }}: {{ report_csv | csv | json }}
}
}
{% endaction %}
{% endif %}