-
Notifications
You must be signed in to change notification settings - Fork 35
/
script.liquid
283 lines (244 loc) · 8.83 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
{% assign out_of_stock_inventory_quantity = options.out_of_stock_inventory_quantity__number_required %}
{% assign search_query = options.only_monitor_products_matching_this_search_query | default: "" %}
{% assign metafield_key
= task.id
| append: search_query
| append: out_of_stock_inventory_quantity
| sha256
| slice: 0, 7
| append: "-out-of-stock"
%}
{% if event.topic contains "shopify/inventory_levels/" %}
{% capture query %}
query {
inventoryLevel(
id: {{ inventory_level.admin_graphql_api_id | json }}
) {
item {
variant {
product {
legacyResourceId
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% comment %}
-- send the product ID through the paginated query so any configured search query can be applied to it
{% endcomment %}
{%- capture search_query -%}
{{ search_query }} id:{{ result.data.inventoryLevel.item.variant.product.legacyResourceId }}
{%- endcapture -%}
{% endif %}
{% assign back_in_stock_email_list = array %}
{% assign newly_back_in_stock_email_product_title = nil %}
{% assign newly_back_in_stock_product_ids = array %}
{% assign newly_out_of_stock_product_ids = array %}
{% assign out_of_stock_email_list = array %}
{% assign out_of_stock_email_product_title = nil %}
{% assign cursor = nil %}
{% for n in (0..100) %}
{% capture query %}
query {
products(
first: 250
query: {{ search_query | json }}
after: {{ cursor | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
legacyResourceId
title
totalInventory
metafield(
namespace: "mechanic"
key: {{ metafield_key | json }}
) {
id
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% for product in result.data.products.nodes %}
{% if product.totalInventory <= out_of_stock_inventory_quantity %}
{% if product.metafield == blank or event.topic == "mechanic/user/trigger" %}
{% log %}
"{{ product.title }} is out of stock."
{% endlog %}
{% assign out_of_stock_email_product_title = product.title %}
{% if product.metafield == blank %}
{% comment %}
For a manual run, we always send an email about out-of-stock products. But, we
don't need to consider these products *newly* out of stock.
{% endcomment %}
{% assign newly_out_of_stock_product_ids[newly_out_of_stock_product_ids.size] = product.id %}
{% endif %}
{%- capture list_item -%}
<li><a href="{{ shop.admin_url }}/products/{{ product.legacyResourceId }}">{{ product.title }}</a></li>
{%- endcapture -%}
{% assign out_of_stock_email_list = out_of_stock_email_list | push: list_item %}
{% endif %}
{% else %}
{% if product.metafield %}
{% log %}
"{{ product.title }} is back in stock."
{% endlog %}
{% assign newly_back_in_stock_email_product_title = product.title %}
{% assign newly_back_in_stock_product_ids[newly_back_in_stock_product_ids.size] = product.id %}
{%- capture list_item -%}
<li><a href="{{ shop.admin_url }}/products/{{ product.legacyResourceId }}">{{ product.title }}</a></li>
{%- endcapture -%}
{% assign back_in_stock_email_list = back_in_stock_email_list | push: list_item %}
{% endif %}
{% endif %}
{% endfor %}
{% if result.products.pageInfo.hasNextPage %}
{% assign cursor = result.data.products.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% if event.preview %}
{% assign out_of_stock_email_list[back_in_stock_email_list.size] = '<li><a>Premium T-Shirt</a></li>' %}
{% assign newly_out_of_stock_product_ids[newly_out_of_stock_product_ids.size] = 'gid://shopify/Product/1234567890' %}
{% assign out_of_stock_email_product_title = 'Premium T-Shirt' %}
{% endif %}
{% log
newly_out_of_stock_product_count: newly_out_of_stock_product_ids.size,
newly_back_in_stock_product_count: newly_back_in_stock_product_ids.size
%}
{% comment %}
-- save mutation inputs for setting and deleting metafields
{% endcomment %}
{% assign metafields_to_set = array %}
{% assign metafields_to_delete = array %}
{% for product_id in newly_out_of_stock_product_ids %}
{% assign metafield_to_set = hash %}
{% assign metafield_to_set["ownerId"] = product_id %}
{% assign metafield_to_set["namespace"] = "mechanic" %}
{% assign metafield_to_set["key"] = metafield_key %}
{% assign metafield_to_set["type"] = "number_integer" %}
{% assign metafield_to_set["value"] = "1" %}
{% assign metafields_to_set = metafields_to_set | push: metafield_to_set %}
{% endfor %}
{% for product_id in newly_back_in_stock_product_ids %}
{% assign metafield_to_delete = hash %}
{% assign metafield_to_delete["ownerId"] = product_id %}
{% assign metafield_to_delete["namespace"] = "mechanic" %}
{% assign metafield_to_delete["key"] = metafield_key %}
{% assign metafields_to_delete = metafields_to_delete | push: metafield_to_delete %}
{% endfor %}
{% if options.send_email_for_out_of_stock_products__boolean != true %}
{% assign out_of_stock_email_list = array %}
{% endif %}
{% if options.send_email_for_back_in_stock_products__boolean != true %}
{% assign back_in_stock_email_list = array %}
{% endif %}
{% if out_of_stock_email_list.size > 0 or back_in_stock_email_list.size > 0 %}
{% capture email_subject %}
{% if out_of_stock_email_list.size == 1 and back_in_stock_email_list.size == 0 %}
Out of stock: {{ out_of_stock_email_product_title }}
{% elsif back_in_stock_email_list.size == 1 and out_of_stock_email_list.size == 0 %}
Back in stock: {{ newly_back_in_stock_email_product_title }}
{% elsif out_of_stock_email_list.size > 1 and back_in_stock_email_list.size == 0 %}
Out of stock: {{ out_of_stock_email_list.size }} products
{% elsif back_in_stock_email_list.size > 1 and out_of_stock_email_list.size == 0 %}
Back in stock: {{ out_of_stock_email_list.size }} products
{% else %}
Stock update: {{ out_of_stock_email_list.size }} out of stock, {{ back_in_stock_email_list.size }} back in stock
{% endif %}
{% endcapture %}
{% capture email_body %}
Hi there,
<br><br>
{% if out_of_stock_email_list.size > 0 %}
We found <b>{{ out_of_stock_email_list.size }} out of stock {{ out_of_stock_email_list.size | pluralize: 'product', 'products' }}</b>:
<br>
<ul>{{ out_of_stock_email_list | join: "" }}</ul>
<br>
{% endif %}
{% if back_in_stock_email_list.size > 0 %}
We found <b>{{ back_in_stock_email_list.size }} back in stock {{ back_in_stock_email_list.size | pluralize: 'product', 'products' }}</b>:
<br>
<ul>{{ back_in_stock_email_list | join: "" }}</ul>
<br>
{% endif %}
Thanks,
<br>Mechanic (for {{ shop.name }})
{% endcapture %}
{% action "email" %}
{
"to": {{ options.stock_update_email_recipients__email_array_required | join: ", " | json }},
"subject": {{ email_subject | strip | json }},
"body": {{ email_body | unindent | strip | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}
{% endif %}
{% comment %}
-- metafields may be set 25 at a time
{% endcomment %}
{% if metafields_to_set != blank %}
{% assign groups_of_metafields_to_set = metafields_to_set | in_groups_of: 25, fill_with: false %}
{% for group_of_metafields_to_set in groups_of_metafields_to_set %}
{% action "shopify" %}
mutation {
metafieldsSet(
metafields: {{ group_of_metafields_to_set | graphql_arguments }}
) {
metafields {
id
namespace
key
type
value
owner {
... on Product {
id
}
}
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% endfor %}
{% endif %}
{% comment %}
-- metafields may be deleted in batches of 250
{% endcomment %}
{% if metafields_to_delete != blank %}
{% assign groups_of_metafields_to_delete = metafields_to_delete | in_groups_of: 250, fill_with: false %}
{% for group_of_metafields_to_delete in groups_of_metafields_to_delete %}
{% action "shopify" %}
mutation {
metafieldsDelete(
metafields: {{ group_of_metafields_to_delete | graphql_arguments }}
) {
deletedMetafields {
ownerId
namespace
key
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endfor %}
{% endif %}