-
Notifications
You must be signed in to change notification settings - Fork 35
/
script.liquid
169 lines (151 loc) · 6.04 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
{% assign required_product_id = options.required_product_id__number_required %}
{% assign discount_collection_id = options.discount_collection_id__number %}
{% assign discount_code_prefix = options.discount_code_prefix %}
{% assign discount_fixed_amount = options.discount_fixed_amount__number %}
{% assign discount_percentage = options.discount_percentage__number %}
{% assign discount_applies_to_each_line_item_individually = options.discount_applies_to_each_line_item_individually__boolean %}
{% assign discount_lifetime_in_days = options.discount_lifetime_in_days__number %}
{% assign discount_can_be_used_by_anyone = options.discount_can_be_used_by_anyone__boolean %}
{% 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 discount_collection_id != blank %}
{% assign discount_collection_id = discount_collection_id | prepend: "gid://shopify/Collection/" %}
{% endif %}
{% if event.topic == "shopify/orders/paid" %}
{% if event.preview %}
{% capture order_json %}
{
"email": "[email protected]",
"customer": {
"id": 1234567890
},
"line_items": [
{
"id": 1234567890,
"product_id": {{ required_product_id }},
"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/" %}
{% comment %}
-- create a discount code for each quantity of matching line items
{% endcomment %}
{% for line_item in order.line_items %}
{% if line_item.product_id != required_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 %}
{% action "shopify" %}
mutation {
discountCodeBasicCreate(
basicCodeDiscount: {
code: {{ discount_code | json }}
customerSelection: {
{% if discount_can_be_used_by_anyone %}
all: true
{% else %}
customers: {
add: {{ array | push: customer_id | json }}
}
{% endif %}
}
title: {{ discount_code | json }}
startsAt: {{ "now" | date: "%FT%T%:z" | json }}
{% if discount_lifetime_in_days != blank %}
{% assign validity_days_s = 60 | times: 60 | times: 24 | times: discount_lifetime_in_days %}
endsAt: {{ "now" | date: "%s" | plus: validity_days_s | date: "%FT%T%:z" | json }}
{% endif %}
customerGets: {
items: {
{% if discount_collection_id != blank %}
collections: {
add: {{ array | push: discount_collection_id | json }}
}
{% else %}
all: true
{% endif %}
}
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 }}
{% if discount_collection_id != blank %}
appliesOnEachItem: {{ discount_applies_to_each_line_item_individually }}
{% else %}
appliesOnEachItem: false
{% endif %}
}
{% endif %}
}
}
}
) {
codeDiscountNode {
id
codeDiscount {
... on DiscountCodeBasic {
codes(first: 1) {
nodes {
id
code
}
}
endsAt
summary
}
}
}
userErrors {
code
extraInfo
field
message
}
}
}
{% 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 %}
{% comment %}
-- send a customer email for the created discount code
{% endcomment %}
{% assign discount_code = action.run.result.data.discountCodeBasicCreate.codeDiscountNode.codeDiscount.codes.nodes.first.code %}
{% assign email_subject = email_subject | replace: 'DISCOUNT_CODE', discount_code %}
{% assign email_body = email_body | replace: 'DISCOUNT_CODE', discount_code %}
{% action "email" %}
{
"to": {{ event.parent.data.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 %}