forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.liquid
96 lines (86 loc) · 3.93 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
{% assign ve_product_ids_keyval = options.voucher_product_ids_and_entitled_product_ids__keyval_number_required %}
{% if event.preview or order.customer %}
{% for order_line_item in order.line_items %}
{% assign voucher_product_id = order_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 event.preview or entitled_product_id %}
{% for order_line_item_n in (1..order_line_item.quantity) %}
{% assign discount_code = options.discount_code_prefix__required | append: order.order_number | append: order_line_item.sku | append: order_line_item_n | append: order.customer.id %}
{% if event.preview or shop.discount_codes[discount_code] == nil %}
{% assign voucher_product = shop.products[voucher_product_id] %}
{% assign entitled_product = shop.products[entitled_product_id] %}
{% assign voucher_product_title = voucher_product.title | default: "(voucher product title)" %}
{% assign entitled_product_title = entitled_product.title | default: "(entitled product title)" %}
{% assign entitled_product_admin_graphql_api_id = entitled_product.admin_graphql_api_id %}
{% assign customer_admin_graphql_api_id = shop.customers[order.customer.id].admin_graphql_api_id %}
{% capture graphql_query %}
mutation {
priceRuleCreate(
priceRule: {
allocationMethod: ACROSS
customerSelection: {
customerIdsToAdd: [{{ customer_admin_graphql_api_id | json }}]
}
itemEntitlements:{
productIds: [{{ entitled_product_admin_graphql_api_id | json }}]
}
target: LINE_ITEM
title: {{ discount_code | json }}
usageLimit: 1
validityPeriod: {
start: {{ "now" | date: "%FT%T%:z" | json }}
}
value: {
fixedAmountValue: {{ options.discount_value__number_required | json }}
}
}
priceRuleDiscountCode: {
code: {{ discount_code | json }}
}
) {
priceRule {
id
}
priceRuleUserErrors {
code
field
message
}
priceRuleDiscountCode {
code
id
}
}
}
{% endcapture %}
{
"action": {
"type": "shopify",
"options": {{ graphql_query | json }}
}
}
{% assign email_subject = options.email_subject__required | replace: 'DISCOUNT_CODE', discount_code | replace: 'VOUCHER_PRODUCT_TITLE', voucher_product_title | replace: 'ENTITLED_PRODUCT_TITLE', entitled_product_title %}
{% assign email_body = options.email_body__multiline_required | replace: 'DISCOUNT_CODE', discount_code | replace: 'VOUCHER_PRODUCT_TITLE', voucher_product_title | replace: 'ENTITLED_PRODUCT_TITLE', entitled_product_title %}
{
"action": {
"type": "email",
"options": {
"to": {{ order.email | json }},
"subject": {{ email_subject | strip | json }},
"body": {{ email_body | newline_to_br | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
}
}
{% else %}
{"log": {{ discount_code | append: " already exists; skipping" | json }}}
{% endif %}
{% endfor %}
{% endif %}
{% if event.preview %}
{% break %}
{% endif %}
{% endfor %}
{% endif %}