|
28 | 28 |
|
29 | 29 | {% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %} |
30 | 30 | {% assign run_qualifies = false %} |
| 31 | + {% assign cycle_start_date = cycle_start_date %} |
| 32 | + {% assign cycle_start_d = cycle_start_date | date: "%s" | times: 1 | divided_by: 60 | divided_by: 60 | divided_by: 24 %} |
| 33 | + {% assign now_d = "now" | date: "%s" | times: 1 | divided_by: 60 | divided_by: 60 | divided_by: 24 %} |
| 34 | + {% assign day_gap = now_d | minus: cycle_start_d | modulo: number_of_days_in_cycle %} |
31 | 35 |
|
32 | | - {% if event.preview %} |
33 | | - {% action "shopify" %} |
34 | | - mutation { |
35 | | - draftOrderCreate( |
36 | | - input: { |
37 | | - billingAddress: { |
38 | | - address1: "123 State St" |
39 | | - city: "Everyville" |
40 | | - company: "Widgets Inc" |
41 | | - countryCode: EE |
42 | | - firstName: "Victor" |
43 | | - lastName: "Juarez" |
44 | | - provinceCode: "PK" |
45 | | - zip: "54321" |
46 | | - } |
47 | | - purchasingEntity: { |
48 | | - customerId: "gid://shopify/Customer/1234567890" |
49 | | - } |
50 | | - email: "customer@example.com" |
51 | | - lineItems: [ |
52 | | - { |
53 | | - weight: { |
54 | | - unit: GRAMS |
55 | | - value: 5 |
56 | | - } |
57 | | - originalUnitPriceWithCurrency: { |
58 | | - amount: "5.0" |
59 | | - currencyCode: USD |
60 | | - } |
61 | | - quantity: 2 |
62 | | - requiresShipping: false |
63 | | - sku: "123ABC" |
64 | | - taxable: true |
65 | | - title: "Widget, Our Best" |
66 | | - } |
67 | | - ] |
68 | | - tags: [] |
69 | | - taxExempt: false |
70 | | - useCustomerDefaultAddress: false |
71 | | - } |
72 | | - ) { |
73 | | - draftOrder { |
74 | | - id |
75 | | - } |
76 | | - userErrors { |
77 | | - field |
78 | | - message |
79 | | - } |
80 | | - } |
81 | | - } |
82 | | - {% endaction %} |
83 | | - |
| 36 | + {% if day_gap == 0 or event.preview %} |
| 37 | + {% assign run_qualifies = true %} |
84 | 38 | {% else %} |
85 | | - {% assign cycle_start_date = cycle_start_date %} |
86 | | - {% assign cycle_start_d = cycle_start_date | date: "%s" | times: 1 | divided_by: 60 | divided_by: 60 | divided_by: 24 %} |
87 | | - {% assign now_d = "now" | date: "%s" | times: 1 | divided_by: 60 | divided_by: 60 | divided_by: 24 %} |
88 | | - {% assign day_gap = now_d | minus: cycle_start_d | modulo: number_of_days_in_cycle %} |
89 | | - |
90 | | - {% if day_gap == 0 %} |
91 | | - {% assign run_qualifies = true %} |
92 | | - {% else %} |
93 | | - {% log %} |
94 | | - {{ number_of_days_in_cycle | minus: day_gap | prepend: "Waiting another " | append: " day(s) for cycle to complete" | json }} |
95 | | - {% endlog %} |
96 | | - {% endif %} |
| 39 | + {% log %} |
| 40 | + {{ number_of_days_in_cycle | minus: day_gap | prepend: "Waiting another " | append: " day(s) for cycle to complete" | json }} |
| 41 | + {% endlog %} |
97 | 42 | {% endif %} |
98 | 43 |
|
99 | 44 | {% if run_qualifies %} |
| 45 | + {% assign cursor = nil %} |
| 46 | + |
100 | 47 | {% for n in (0..10) %} |
101 | 48 | {% capture query %} |
102 | 49 | query { |
|
112 | 59 | } |
113 | 60 | nodes { |
114 | 61 | id |
| 62 | + name |
| 63 | + createdAt |
| 64 | + acceptAutomaticDiscounts |
| 65 | + allowDiscountCodesInCheckout |
115 | 66 | appliedDiscount { |
116 | 67 | amountSet { |
117 | 68 | shopMoney { |
|
143 | 94 | key |
144 | 95 | value |
145 | 96 | } |
| 97 | + discountCodes |
146 | 98 | email |
147 | 99 | lineItems(first: 150) { |
148 | 100 | nodes { |
|
211 | 163 |
|
212 | 164 | {% assign result = query | shopify %} |
213 | 165 |
|
| 166 | + {% if event.preview %} |
| 167 | + {% capture result_json %} |
| 168 | + { |
| 169 | + "data": { |
| 170 | + "draftOrders": { |
| 171 | + "nodes": [ |
| 172 | + { |
| 173 | + "id": "gid://shopify/DraftOrder/1234567890", |
| 174 | + "billingAddress": { |
| 175 | + "address1": "123 Main Street", |
| 176 | + "address2": "", |
| 177 | + "city": "Springfield", |
| 178 | + "company": "Widgets Inc", |
| 179 | + "countryCodeV2": "US", |
| 180 | + "firstName": "Marge", |
| 181 | + "lastName": "Simpson", |
| 182 | + "phone": null, |
| 183 | + "provinceCode": "IL", |
| 184 | + "zip": "62701" |
| 185 | + }, |
| 186 | + "customer": { |
| 187 | + "id": "gid://shopify/Customer/1234567890" |
| 188 | + }, |
| 189 | + "email": "customer@example.com", |
| 190 | + "lineItems": { |
| 191 | + "nodes": [ |
| 192 | + { |
| 193 | + "originalUnitPriceWithCurrency": { |
| 194 | + "amount": "5.0", |
| 195 | + "currencyCode": "USD" |
| 196 | + }, |
| 197 | + "quantity": 2, |
| 198 | + "requiresShipping": false, |
| 199 | + "sku": "WIDGET-BEST", |
| 200 | + "taxable": true, |
| 201 | + "title": "Widget, Our Best", |
| 202 | + "weight": { |
| 203 | + "unit": "Grams", |
| 204 | + "value": 5 |
| 205 | + } |
| 206 | + } |
| 207 | + ] |
| 208 | + }, |
| 209 | + "tags": {{ array | push: draft_order_tag, "preview-tag" | json }} |
| 210 | + } |
| 211 | + ] |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + {% endcapture %} |
| 216 | + |
| 217 | + {% assign result = result_json | parse_json %} |
| 218 | + {% endif %} |
| 219 | + |
214 | 220 | {% for draft_order in result.data.draftOrders.nodes %} |
| 221 | + {% log |
| 222 | + message: "Draft order qualifies to be duplicated", |
| 223 | + original_draft_order_data: draft_order |
| 224 | + %} |
| 225 | + |
215 | 226 | {% action "shopify" %} |
216 | 227 | mutation { |
217 | 228 | draftOrderCreate( |
218 | 229 | input: { |
| 230 | + acceptAutomaticDiscounts: {{ draft_order.acceptAutomaticDiscounts | json }} |
| 231 | + allowDiscountCodesInCheckout: {{ draft_order.allowDiscountCodesInCheckout | json }} |
219 | 232 | {% if draft_order.appliedDiscount %} |
220 | 233 | appliedDiscount: { |
221 | | - amountWithCurrency: { |
222 | | - amount: {{ draft_order.appliedDiscount.amountSet.shopMoney.amount | json }} |
223 | | - currencyCode: {{ draft_order.appliedDiscount.amountSet.shopMoney.currencyCode }} |
224 | | - } |
225 | 234 | description: {{ draft_order.appliedDiscount.description | json }} |
226 | 235 | title: {{ draft_order.appliedDiscount.title | json }} |
227 | 236 | value: {{ draft_order.appliedDiscount.value }} |
|
257 | 266 | {% endfor %} |
258 | 267 | ] |
259 | 268 | {% endif %} |
| 269 | + {% if draft_order.discountCodes != blank %} |
| 270 | + discountCodes: {{ draft_order.discountCodes | json }} |
| 271 | + {% endif %} |
260 | 272 | {% if draft_order.email %} |
261 | 273 | email: {{ draft_order.email | json }} |
262 | 274 | {% endif %} |
|
265 | 277 | { |
266 | 278 | {% if line_item.appliedDiscount %} |
267 | 279 | appliedDiscount: { |
268 | | - amountWithCurrency: { |
269 | | - amount: {{ line_item.appliedDiscount.amountSet.shopMoney.amount | json }} |
270 | | - currencyCode: {{ line_item.appliedDiscount.amountSet.shopMoney.currencyCode }} |
271 | | - } |
272 | 280 | description: {{ line_item.appliedDiscount.description | json }} |
273 | 281 | title: {{ line_item.appliedDiscount.title | json }} |
274 | 282 | value: {{ line_item.appliedDiscount.value | json }} |
|
392 | 400 |
|
393 | 401 | {% assign result = query | shopify %} |
394 | 402 |
|
395 | | - {% if event.preview or result.data.draftOrder.metafield %} |
396 | | - {% action "shopify" %} |
397 | | - mutation { |
398 | | - draftOrderInvoiceSend( |
399 | | - id: {{ draft_order.admin_graphql_api_id | json }} |
400 | | - email: { |
401 | | - {% if email_invoice_subject != blank %} |
402 | | - subject: {{ email_invoice_subject | json }} |
403 | | - {% endif %} |
404 | | - bcc: {{ email_invoice_bcc | json }} |
405 | | - customMessage: {{ email_invoice_custom_message | json }} |
406 | | - } |
407 | | - ) { |
408 | | - userErrors { |
409 | | - field |
410 | | - message |
411 | | - } |
| 403 | + {% if draft_order.email == blank or result.data.draftOrder.metafield == blank %} |
| 404 | + {% unless event.preview %} |
| 405 | + {% log "Draft order does not contain the Mechanic autoinvoice metafield or the draft order does not have an email; no invoice will be sent." %} |
| 406 | + {% break %} |
| 407 | + {% endunless %} |
| 408 | + {% endif %} |
| 409 | + |
| 410 | + {% action "shopify" %} |
| 411 | + mutation { |
| 412 | + draftOrderInvoiceSend( |
| 413 | + id: {{ draft_order.admin_graphql_api_id | json }} |
| 414 | + email: { |
| 415 | + {% if email_invoice_subject != blank %} |
| 416 | + subject: {{ email_invoice_subject | json }} |
| 417 | + {% endif %} |
| 418 | + bcc: {{ email_invoice_bcc | json }} |
| 419 | + customMessage: {{ email_invoice_custom_message | json }} |
| 420 | + } |
| 421 | + ) { |
| 422 | + userErrors { |
| 423 | + field |
| 424 | + message |
412 | 425 | } |
413 | 426 | } |
414 | | - {% endaction %} |
415 | | - {% endif %} |
| 427 | + } |
| 428 | + {% endaction %} |
416 | 429 |
|
417 | 430 | {% elsif event.topic == "shopify/draft_orders/create" and autocomplete %} |
418 | 431 | {% capture query %} |
|
0 commit comments