-
Notifications
You must be signed in to change notification settings - Fork 35
/
script.liquid
222 lines (197 loc) · 7.26 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
{% assign ve_product_ids_keyval = options.voucher_product_ids_and_entitled_product_ids__keyval_number_required %}
{% assign discount_code_prefix = options.discount_code_prefix__required %}
{% assign discount_fixed_amount = options.discount_fixed_amount__number %}
{% assign discount_percentage = options.discount_percentage__number %}
{% assign email_subject = options.email_subject__required %}
{% assign email_body = options.email_body__multiline_required %}
{% if discount_percentage == blank and discount_fixed_amount == blank %}
{% error "Please fill either the discount percentage or discount fixed amount." %}
{% elsif discount_percentage != blank and discount_fixed_amount != blank %}
{% error "Please choose between the discount percentage and discount fixed amount - only one is permitted." %}
{% endif %}
{% if event.topic == "shopify/orders/paid" %}
{% if event.preview %}
{% capture order_json %}
{
"email": "[email protected]",
"customer": {
"id": 1234567890,
"first_name": "TEST"
},
"line_items": [
{
"id": 1234567890,
"product_id": {{ ve_product_ids_keyval.first.first }},
"quantity": 1
}
]
}
{% endcapture %}
{% assign order = order_json | parse_json %}
{% endif %}
{% if order.customer == blank %}
{% log "This order has no related customer. Skipping" %}
{% break %}
{% endif %}
{% if order.email == blank %}
{% log "This order has no email address. Skipping" %}
{% break %}
{% endif %}
{% assign customer_id = order.customer.id | prepend: "gid://shopify/Customer/" %}
{% for line_item in order.line_items %}
{% assign voucher_product_title = line_item.title %}
{% assign voucher_product_id = line_item.product_id %}
{% assign voucher_product_id_string = voucher_product_id | append: "" %}
{% assign entitled_product_id = ve_product_ids_keyval[voucher_product_id_string] %}
{% if entitled_product_id == blank %}
{% continue %}
{% endif %}
{% comment %}
-- make sure the entitled product exists in the shop and get its title
{% endcomment %}
{% capture query %}
query {
product(id: {{ entitled_product_id | prepend: "gid://shopify/Product/" | json }}) {
id
title
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"product": {
"id": "gid://shopify/Product/1234567890",
"title": "Widget"
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign entitled_product = result.data.product %}
{% if entitled_product == blank %}
{% action "echo"
__error: "Entitled product not found in the shop. Discount code will not be created.",
entitled_product_id: entitled_product_id,
voucher_product_id: voucher_product_id
%}
{% continue %}
{% endif %}
{% for n in (1..line_item.quantity) %}
{% assign discount_code = line_item.id | append: n | split: "" | reverse | join: "" | slice: 0, 4 | append: order.order_number | base64 | replace: "=", "" | upcase | prepend: discount_code_prefix %}
{% capture mutation %}
mutation {
discountCodeBasicCreate(
basicCodeDiscount: {
code: {{ discount_code | json }}
customerSelection: {
customers: {
add: {{ array | push: customer_id | json }}
}
}
title: {{ discount_code | json }}
startsAt: {{ "now" | date: "%FT%T%:z" | json }}
customerGets: {
items: {
products: {
productsToAdd: {{ array | push: entitled_product.id | json }}
}
}
value: {
{% if discount_percentage != blank %}
percentage: {{ discount_percentage | abs | divided_by: 100.0 | json }}
{% else %}
discountAmount: {
amount: {{ discount_fixed_amount | abs | times: 1.0 | json }}
appliesOnEachItem: false
}
{% endif %}
}
}
usageLimit: 1
}
) {
codeDiscountNode {
id
codeDiscount {
... on DiscountCodeBasic {
codes(first: 1) {
nodes {
id
code
}
}
summary
}
}
}
userErrors {
code
extraInfo
field
message
}
}
}
{% endcapture %}
{% action %}
{
"type": "shopify",
"options": {
"query": {{ mutation | json }}
},
"meta": {
"customer_first_name": {{ order.customer.first_name | default: "there" | json }},
"order_email": {{ order.email | json }},
"order_name": {{ order.name | json }},
"discount_code": {{ discount_code | json }},
"entitled_product_title": {{ entitled_product.title | json }},
"voucher_product_title": {{ voucher_product_title | json }}
}
}
{% endaction %}
{% endfor %}
{% endfor %}
{% elsif event.topic == "mechanic/actions/perform" %}
{% comment %}
-- only process a successful discountCodeBasicCreate shopify action; the Error reporter task should be used to respond to failures
{% endcomment %}
{% unless action.type == "shopify" and action.run.ok == true %}
{% break %}
{% endunless %}
{% assign customer_first_name = action.meta.customer_first_name %}
{% assign order_email = action.meta.order_email %}
{% assign order_name = action.meta.order_name %}
{% assign discount_code = action.meta.discount_code %}
{% assign entitled_product_title = action.meta.entitled_product_title %}
{% assign voucher_product_title = action.meta.voucher_product_title %}
{% comment %}
-- send a customer email for the created discount code
{% endcomment %}
{% assign email_subject = email_subject
| replace: 'CUSTOMER_FIRST_NAME', customer_first_name
| replace: 'DISCOUNT_CODE', discount_code
| replace: 'ENTITLED_PRODUCT_TITLE', entitled_product_title
| replace: 'ORDER_NAME', order_name
| replace: 'VOUCHER_PRODUCT_TITLE', voucher_product_title
%}
{% assign email_body = email_body
| replace: 'CUSTOMER_FIRST_NAME', customer_first_name
| replace: 'DISCOUNT_CODE', discount_code
| replace: 'ENTITLED_PRODUCT_TITLE', entitled_product_title
| replace: 'ORDER_NAME', order_name
| replace: 'VOUCHER_PRODUCT_TITLE', voucher_product_title
%}
{% action "email" %}
{
"to": {{ order_email | json }},
"subject": {{ email_subject | strip | json }},
"body": {{ email_body | strip | newline_to_br | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}
{% endif %}