forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.liquid
161 lines (141 loc) · 3.91 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
{% assign metafield_namespace = options.metafield_namespace__required %}
{% assign metafield_key = options.metafield_key__required %}
{% assign donation_product_handle = options.donation_product_handle__required %}
{% capture query %}
query {
shop {
id
metafield(
namespace: {{ metafield_namespace | json }}
key: {{ metafield_key | json }}
) {
value
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"shop": {
"id": "gid://shopify/Shop/1234567890",
"metafield": {
"value": "123.45"
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign shop = result.data.shop %}
{% assign metafield_donation_total = shop.metafield.value | times: 1 %}
{% assign donation_total = 0 %}
{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
{% assign cursor = nil %}
{% for n in (0..10000) %}
{% capture query %}
query {
orders(
first: 10
after: {{ cursor | json }}
query: "financial_status:paid"
) {
pageInfo {
hasNextPage
}
edges {
cursor
node {
id
lineItems(first: 25) {
edges {
node {
id
product {
handle
}
discountedUnitPriceSet {
shopMoney {
amount
}
}
}
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% assign orders = result.data.orders.edges | map: "node" %}
{% for order in orders %}
{% assign line_items = order.lineItems.edges | map: "node" %}
{% for line_item in line_items %}
{% if line_item.product.handle != donation_product_handle %}
{% continue %}
{% endif %}
{% assign donation_total = donation_total | plus: line_item.discountedUnitPriceSet.shopMoney.amount %}
{% endfor %}
{% endfor %}
{% if result.data.orders.pageInfo.hasNextPage %}
{% assign cursor = result.data.orders.edges.last.cursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% elsif event.topic == "shopify/orders/create" or event.topic == "shopify/orders/paid" %}
{% assign donation_total = metafield_donation_total %}
{% if order.financial_status == "paid" %}
{% assign current_donation = 0 %}
{% for line_item in order.line_items %}
{% if line_item.product.handle != donation_product_handle %}
{% continue %}
{% endif %}
{% assign current_donation = current_donation | plus: line_item.price | minus: line_item.total_discount %}
{% endfor %}
{% if current_donation > 0 %}
{% assign donation_total = donation_total | plus: current_donation %}
{% endif %}
{% endif %}
{% endif %}
{% if donation_total != metafield_donation_total or event.preview %}
{% action "shopify" %}
mutation {
metafieldsSet(
metafields: [
{
ownerId: {{ shop.id | json }}
namespace: {{ metafield_namespace | json }}
key: {{ metafield_key | json }}
value: {{ donation_total | append: "" | json }}
type: "number_decimal"
}
]
) {
metafields {
id
namespace
key
type
value
owner {
... on Shop {
id
name
myshopifyDomain
}
}
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% endif %}