-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathscript.liquid
124 lines (112 loc) · 3.46 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
{% assign tag_to_match = options.required_customer_tag__required | downcase %}
{% if event.topic contains "shopify/orders" %}
{% assign customer_tags = order.customer.tags | downcase | split: ", " %}
{% capture order_query %}
query {
order(id: {{ order.admin_graphql_api_id | json }}) {
canMarkAsPaid
}
}
{% endcapture %}
{% assign order_result = order_query | shopify %}
{% if event.preview or customer_tags contains tag_to_match %}
{% if order_result.data.order.canMarkAsPaid == false %}
{"log": "Order cannot be manually marked as paid - skipping."}
{% else %}
{% action "shopify" %}
mutation {
orderMarkAsPaid(input: {
id: {{ order.admin_graphql_api_id | json }}
}) {
order {
fullyPaid
}
userErrors {
field
message
}
}
{% if options.remove_tag_from_customer_after_processing_their_order__boolean %}
tagsRemove(
id: {% if event.preview %}"gid://shopify/Customer/1234567890"{% else %}{{ order.customer.admin_graphql_api_id | json }}{% endif %}
tags: {{ options.required_customer_tag__required | json }}
) {
userErrors {
field
message
}
}
{% endif %}
}
{% endaction %}
{% endif %}
{% endif %}
{% elsif event.topic == "mechanic/user/trigger" %}
{% assign cursor = nil %}
{% for n in (0..100) %}
{% capture orders_query %}
query {
orders(
first: 250
after: {{ cursor | json }}
query: "-financial_status:paid"
) {
pageInfo {
hasNextPage
}
edges {
cursor
node {
id
canMarkAsPaid
customer {
id
tags
}
}
}
}
}
{% endcapture %}
{% assign orders_result = orders_query | shopify %}
{% for edge in orders_result.data.orders.edges %}
{% assign order_node = edge.node %}
{% if order_node.canMarkAsPaid %}
{% assign customer_tags = order_node.customer.tags | join: ", " | downcase | split: ", " %}
{% if customer_tags contains tag_to_match %}
{% action "shopify" %}
mutation {
orderMarkAsPaid(input: {
id: {{ order_node.id | json }}
}) {
order {
fullyPaid
}
userErrors {
field
message
}
}
{% if options.remove_tag_from_customer_after_processing_their_order__boolean %}
tagsRemove(
id: {% if event.preview %}"gid://shopify/Customer/1234567890"{% else %}{{ order_node.customer.id | json }}{% endif %}
tags: {{ options.required_customer_tag__required | json }}
) {
userErrors {
field
message
}
}
{% endif %}
}
{% endaction %}
{% endif %}
{% endif %}
{% endfor %}
{% if orders_result.data.orders.pageInfo.hasNextPage %}
{% assign cursor = orders_result.data.orders.edges.last.cursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% endif %}