Skip to content

Commit 10d2f5a

Browse files
authored
use discount value instead of applied discount amount (#506)
1 parent e766c15 commit 10d2f5a

File tree

3 files changed

+115
-98
lines changed

3 files changed

+115
-98
lines changed

docs/auto-recurring-draft-orders/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Tags: Draft Orders, Recurring
44

5-
This task searches for draft orders having the configured tag, and duplicates each one (minus the tag used for searching). Optionally, this task can automatically send an invoice to the customer on file, after the new draft order is created. Or, this task can auto-complete the order, marking the order as paid if you so chose. (Auto-completed orders will result in an email receipt being sent to the customer on file for the original draft order.)
5+
This task searches for draft orders having the configured tag, and duplicates each one (minus the tag used for searching). Optionally, this task can automatically send an invoice to the customer on file, after the new draft order is created. Or, this task can auto-complete the order, marking the order as paid if you so chose. (Auto-completed orders will result in a email receipt being sent to the customer on file for the original draft order.)
66

77
* View in the task library: [tasks.mechanic.dev/auto-recurring-draft-orders](https://tasks.mechanic.dev/auto-recurring-draft-orders)
88
* Task JSON, for direct import: [task.json](../../tasks/auto-recurring-draft-orders.json)
@@ -31,16 +31,20 @@ This task searches for draft orders having the configured tag, and duplicates ea
3131
```liquid
3232
mechanic/user/trigger
3333
mechanic/scheduler/daily
34-
{% if options.send_email_invoice_after_creating__boolean or options.complete_the_order_after_creating__boolean or options.complete_the_order_and_mark_as_paid_after_creating__boolean %}shopify/draft_orders/create{% endif %}
34+
{% if options.send_email_invoice_after_creating__boolean or options.complete_the_order_after_creating__boolean or options.complete_the_order_and_mark_as_paid_after_creating__boolean %}
35+
shopify/draft_orders/create
36+
{% endif %}
3537
```
3638

3739
[Learn about event subscriptions in Mechanic](https://learn.mechanic.dev/core/tasks/subscriptions)
3840

3941
## Documentation
4042

41-
This task searches for draft orders having the configured tag, and duplicates each one (minus the tag used for searching). Optionally, this task can automatically send an invoice to the customer on file, after the new draft order is created. Or, this task can auto-complete the order, marking the order as paid if you so chose. (Auto-completed orders will result in an email receipt being sent to the customer on file for the original draft order.)
43+
This task searches for draft orders having the configured tag, and duplicates each one (minus the tag used for searching). Optionally, this task can automatically send an invoice to the customer on file, after the new draft order is created. Or, this task can auto-complete the order, marking the order as paid if you so chose. (Auto-completed orders will result in a email receipt being sent to the customer on file for the original draft order.)
4244

43-
Run this task manually to scan and duplicate matching draft orders. Use the "run" task options to control automatic running of this task.
45+
Use the "Cycle start date" and "Number of days in cycle" options to control the frequency of the recurring invoices. The task may also be run manually, but the cycle will still be checked to make sure the current day is valid for the cycle.
46+
47+
**Note**: When duplicating invoices, this task will include most discount applications from the original draft order, including custom order and line item discounts, discount codes, and whether or not automatic discounts can be applied.
4448

4549
## Installing this task
4650

docs/auto-recurring-draft-orders/script.liquid

Lines changed: 104 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -28,75 +28,22 @@
2828

2929
{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
3030
{% 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 %}
3135

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 %}
8438
{% 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 %}
9742
{% endif %}
9843

9944
{% if run_qualifies %}
45+
{% assign cursor = nil %}
46+
10047
{% for n in (0..10) %}
10148
{% capture query %}
10249
query {
@@ -112,6 +59,10 @@
11259
}
11360
nodes {
11461
id
62+
name
63+
createdAt
64+
acceptAutomaticDiscounts
65+
allowDiscountCodesInCheckout
11566
appliedDiscount {
11667
amountSet {
11768
shopMoney {
@@ -143,6 +94,7 @@
14394
key
14495
value
14596
}
97+
discountCodes
14698
email
14799
lineItems(first: 150) {
148100
nodes {
@@ -211,17 +163,74 @@
211163

212164
{% assign result = query | shopify %}
213165

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+
214220
{% 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+
215226
{% action "shopify" %}
216227
mutation {
217228
draftOrderCreate(
218229
input: {
230+
acceptAutomaticDiscounts: {{ draft_order.acceptAutomaticDiscounts | json }}
231+
allowDiscountCodesInCheckout: {{ draft_order.allowDiscountCodesInCheckout | json }}
219232
{% if draft_order.appliedDiscount %}
220233
appliedDiscount: {
221-
amountWithCurrency: {
222-
amount: {{ draft_order.appliedDiscount.amountSet.shopMoney.amount | json }}
223-
currencyCode: {{ draft_order.appliedDiscount.amountSet.shopMoney.currencyCode }}
224-
}
225234
description: {{ draft_order.appliedDiscount.description | json }}
226235
title: {{ draft_order.appliedDiscount.title | json }}
227236
value: {{ draft_order.appliedDiscount.value }}
@@ -257,6 +266,9 @@
257266
{% endfor %}
258267
]
259268
{% endif %}
269+
{% if draft_order.discountCodes != blank %}
270+
discountCodes: {{ draft_order.discountCodes | json }}
271+
{% endif %}
260272
{% if draft_order.email %}
261273
email: {{ draft_order.email | json }}
262274
{% endif %}
@@ -265,10 +277,6 @@
265277
{
266278
{% if line_item.appliedDiscount %}
267279
appliedDiscount: {
268-
amountWithCurrency: {
269-
amount: {{ line_item.appliedDiscount.amountSet.shopMoney.amount | json }}
270-
currencyCode: {{ line_item.appliedDiscount.amountSet.shopMoney.currencyCode }}
271-
}
272280
description: {{ line_item.appliedDiscount.description | json }}
273281
title: {{ line_item.appliedDiscount.title | json }}
274282
value: {{ line_item.appliedDiscount.value | json }}
@@ -392,27 +400,32 @@
392400

393401
{% assign result = query | shopify %}
394402

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
412425
}
413426
}
414-
{% endaction %}
415-
{% endif %}
427+
}
428+
{% endaction %}
416429

417430
{% elsif event.topic == "shopify/draft_orders/create" and autocomplete %}
418431
{% capture query %}

0 commit comments

Comments
 (0)