-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathscript.liquid
325 lines (281 loc) · 9.37 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
{% assign bundle_sku = options.bundle_parent_sku__required %}
{% assign component_skus_and_quantities = options.component_skus_and_quantities_per_bundle__keyval_number_required %}
{% assign discount_message = options.discount_message_to_display_on_each_added_component__required %}
{% assign staff_note = options.staff_note_for_the_order_edit %}
{% assign alert_email_recipients = options.alert_email_recipients__array_required %}
{% assign component_skus = component_skus_and_quantities | keys %}
{% assign component_quantities = component_skus_and_quantities | values %}
{% for component_quantity in component_quantities %}
{% unless component_quantity > 0 %}
{% error "All component quantities must be greater than zero" %}
{% endunless %}
{% endfor %}
{% if event.topic == "shopify/orders/create" or event.topic == "shopify/orders/paid" or event.topic == "mechanic/user/order" %}
{% if event.topic == "mechanic/user/order" %}
{% log "CAUTION: Sending orders manually to this task via the admin link is provided for controlled testing; however, these are edits to live orders. Once you have thoroughly tested this task, uncheck 'Manual mode' so the bundle components can be added when qualifying orders are placed." %}
{% endif %}
{% if event.preview %}
{% capture order_json %}
{
"admin_graphql_api_id": "gid://shopify/Order/1234567890",
"line_items": [
{
"sku": {{ bundle_sku | json }},
"quantity": 1
}
]
}
{% endcapture %}
{% assign order = order_json | parse_json %}
{% endif %}
{% assign bundle_parent_quantity
= order.line_items
| where: "sku", bundle_sku
| map: "quantity"
| sum
%}
{% unless bundle_parent_quantity > 0 %}
{% log %}
{
"message": "No SKUs on this order match the configured bundle SKU.",
"bundle_sku": {{ bundle_sku | json }},
"order_skus": {{ order.line_items | map: "sku" | json }}
}
{% endlog %}
{% break %}
{% endunless %}
{% assign component_variants = array %}
{% for component_sku in component_skus %}
{% capture query %}
query {
productVariants(
first: 2
query: {{ component_sku | json | prepend: "sku:" | json }}
) {
nodes {
id
legacyResourceId
sku
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"productVariants": {
"nodes": [
{
"id": "gid://shopify/ProductVariant/1234567890",
"legacyResourceId": "1234567890",
"sku": {{ component_sku | json }}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign variants = result.data.productVariants.nodes %}
{% if variants == blank %}
{% error
message: "Component SKU does not match a variant in this shop.",
component_sku: component_sku
%}
{% elsif variants.size > 1 %}
{% error
message: "Component SKU matches multiple variants in this shop.",
component_sku: component_sku,
variants: variants
%}
{% else %}
{% assign component_variant = hash %}
{% assign component_variant["id"] = variants.first.id %}
{% assign component_variant["legacyResourceId"] = variants.first.legacyResourceId %}
{% assign component_variant["quantity"] = bundle_parent_quantity | times: component_skus_and_quantities[component_sku] %}
{% assign component_variants = component_variants | push: component_variant %}
{% endif %}
{% endfor %}
{% comment %}
-- begin the order edit and pass the component variants as meta information
{% endcomment %}
{% capture order_edit_begin_query %}
mutation {
orderEditBegin(
id: {{ order.admin_graphql_api_id | json }}
) {
calculatedOrder {
id
originalOrder {
name
}
}
userErrors {
field
message
}
}
}
{% endcapture %}
{% action %}
{
"type": "shopify",
"options": {
"query": {{ order_edit_begin_query | json }}
},
"meta": {
"mutation_type": "orderEditBegin",
"component_variants": {{ component_variants | json }}
}
}
{% endaction %}
{% elsif event.topic == "mechanic/actions/perform" %}
{% comment %}
-- NOTE: task preview events have been configured for results of orderEditBegin, orderEditAddVariant, and orderEditAddLineItemDiscount
{% endcomment %}
{% unless action.run.ok %}
{% comment %}
-- send an alert email for any errors on action runs
{% endcomment %}
{% capture alert_email_subject -%}
ALERT - Error occured while running the Mechanic task: {{ task.name }}
{%- endcapture %}
{% capture alert_email_body -%}
Review the <a href="https://{{ shop.myshopify_domain }}/admin/apps/mechanic/events/{{ event.id }}">task run log</a> to determine next steps.
<strong>Error summary (if any):</strong>
{{ action.run.error }}
{%- endcapture %}
{% action "email" %}
{
"to": {{ alert_email_recipients | json }},
"subject": {{ alert_email_subject | json }},
"body": {{ alert_email_body | newline_to_br | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}
{% break %}
{% endunless %}
{% if action.meta.mutation_type == "orderEditBegin" %}
{% comment %}
-- add all of the component variants to the order
{% endcomment %}
{% assign calculated_order = action.run.result.data.orderEditBegin.calculatedOrder %}
{% assign add_variant_mutations = array %}
{% for component_variant in action.meta.component_variants %}
{% capture add_variant_mutation %}
orderEditAddVariant_{{ component_variant.legacyResourceId }}: orderEditAddVariant(
id: {{ calculated_order.id | json }}
variantId: {{ component_variant.id | json }}
quantity: {{ component_variant.quantity }}
allowDuplicates: true
) {
calculatedLineItem {
id
sku
variant {
legacyResourceId
}
}
userErrors {
field
message
}
}
{% endcapture %}
{% assign add_variant_mutations = add_variant_mutations | push: add_variant_mutation %}
{% endfor %}
{% capture add_variant_query %}
mutation {
{{ add_variant_mutations | join: newline }}
}
{% endcapture %}
{% action %}
{
"type": "shopify",
"options": {
"query": {{ add_variant_query | json }}
},
"meta": {
"mutation_type": "orderEditAddVariant",
"calculated_order": {{ calculated_order | json }}
}
}
{% endaction %}
{% elsif action.meta.mutation_type == "orderEditAddVariant" %}
{% comment %}
-- add 100% line item discounts after adding component variants
{% endcomment %}
{% assign calculated_order = action.meta.calculated_order %}
{% assign add_line_item_discount_mutations = array %}
{% for add_variant_result in action.run.result.data %}
{% assign added_line_item = add_variant_result[1].calculatedLineItem %}
{% capture add_line_item_discount_mutation %}
orderEditAddLineItemDiscount_{{ added_line_item.variant.legacyResourceId }}: orderEditAddLineItemDiscount(
id: {{ calculated_order.id | json }}
lineItemId: {{ added_line_item.id | json }}
discount: {
description: {{ discount_message | json }}
percentValue: 100
}
) {
calculatedLineItem {
id
discountedUnitPriceSet {
shopMoney {
amount
}
}
}
userErrors {
field
message
}
}
{% endcapture %}
{% assign add_line_item_discount_mutations = add_line_item_discount_mutations | push: add_line_item_discount_mutation %}
{% endfor %}
{% capture add_line_item_discount_query %}
mutation {
{{ add_line_item_discount_mutations | join: newline }}
}
{% endcapture %}
{% action %}
{
"type": "shopify",
"options": {
"query": {{ add_line_item_discount_query | json }}
},
"meta": {
"mutation_type": "orderEditAddLineItemDiscount",
"calculated_order": {{ calculated_order | json }}
}
}
{% endaction %}
{% elsif action.meta.mutation_type == "orderEditAddLineItemDiscount" %}
{% comment %}
-- commit the order edit after adding line item discounts
{% endcomment %}
{% assign calculated_order = action.meta.calculated_order %}
{% action "shopify" %}
mutation {
orderEditCommit(
id: {{ calculated_order.id | json }}
notifyCustomer: false
staffNote: {{ staff_note | json }}
) {
order {
id
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endif %}