-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathscript.liquid
148 lines (127 loc) · 3.92 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
{% assign cancellation_reason = options.cancellation_reason_to_set | default: "other" %}
{% assign ignore_unpaid_orders = options.ignore_unpaid_orders__boolean %}
{% assign refund_payment = options.refund_payment_for_cancelled_orders__boolean %}
{% assign restock_inventory = options.restock_inventory_for_cancelled_orders__boolean %}
{% assign notify_customer = options.email_customer_when_cancelling__boolean %}
{% assign staff_note = options.staff_note_for_timeline %}
{% assign order_tag_to_apply = options.add_this_order_tag_when_cancelling %}
{% comment %}
-- check that a valid cancellation reason has been configured; it will default to 'other' if left blank
{% endcomment %}
{% assign valid_cancellation_reasons = "customer,declined,fraud,inventory,other,staff" | split: "," %}
{% unless valid_cancellation_reasons contains cancellation_reason %}
{% error %}
{{ "Cancellation reason " | append: cancellation_reason | append: " - must be one of 'customer', 'declined', 'fraud', 'inventory', 'other', or 'staff'." | json }}
{% enderror %}
{% endunless %}
{% comment %}
-- get the order statuses and risk assessments
{% endcomment %}
{% capture query %}
query {
order(id: {{ order.admin_graphql_api_id | json }}) {
id
name
cancelledAt
displayFinancialStatus
displayFulfillmentStatus
risk {
assessments {
riskLevel
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"order": {
"id": "gid://shopify/Order/1234567890",
"displayFinancialStatus": "PAID",
"displayFulfillmentStatus": "UNFULFILLED",
"risk": {
"assessments": [
{
"riskLevel": "NONE"
},
{
"riskLevel": "HIGH"
}
]
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign order = result.data.order %}
{% log order: order %}
{% if order.cancelledAt %}
{% log "This order has already been cancelled." %}
{% break %}
{% endif %}
{% if ignore_unpaid_orders and order.displayFinancialStatus != "PAID" %}
{% log "This order has not been paid and the ignore unpaid orders option is enabled." %}
{% break %}
{% endif %}
{% comment %}
-- get the risk level from each risk assessment
{% endcomment %}
{% assign order_risk_levels = order.risk.assessments | map: "riskLevel" %}
{% unless order_risk_levels contains "HIGH" %}
{% log "The current risk assessments for this order do not contain a HIGH risk level." %}
{% break %}
{% endunless %}
{% comment %}
-- check to make sure order is unfulfilled to avoid cancellation error
{% endcomment %}
{% if order.displayFulfillmentStatus == "FULFILLED" or order.displayFulfillmentStatus == "PARTIALLY_FULFILLED" %}
{% log "This order has already been fulfilled or partially fulfilled and cannot be cancelled." %}
{% break %}
{% endif %}
{% comment %}
-- cancel the order with configured options
{% endcomment %}
{% action "shopify" %}
mutation {
orderCancel(
orderId: {{ order.id | json }}
notifyCustomer: {{ notify_customer | json }}
reason: {{ cancellation_reason | upcase }}
refund: {{ refund_payment | json }}
restock: {{ restock_inventory | json }}
staffNote: {{ staff_note | json }}
) {
job {
id
}
orderCancelUserErrors {
code
field
message
}
}
}
{% endaction %}
{% comment %}
-- tag the order if there is a tag configured to apply on cancellation
{% endcomment %}
{% if order_tag_to_apply != blank %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ order.id | json }}
tags: {{ order_tag_to_apply | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}