forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.liquid
127 lines (115 loc) · 3.68 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
{% comment %}
An opinion about option order
{{ options.query_parameter_name__required }}
{{ options.cart_attribute_name__required }}
{{ options.tag_customer_with_parameter_value__boolean }}
{{ options.only_tag_the_customer_for_their_first_order__boolean }}
{% endcomment %}
{% if event.preview %}
{% assign order = hash %}
{% assign order["admin_graphql_api_id"] = "gid://shopify/Order/1234567890" %}
{% assign order["note_attributes"] = hash %}
{% assign order["note_attributes"][options.cart_attribute_name__required] = "ABC123" %}
{% assign order["customer"] = hash %}
{% assign order["customer"]["admin_graphql_api_id"] = "gid://shopify/Customer/1234567890" %}
{% endif %}
{% assign value = order.note_attributes[options.cart_attribute_name__required] %}
{% if value == blank %}
{% log message: "This order did not have the specified cart attribute - nothing to do.", attributes: order.note_attributes %}
{% else %}
{% assign order_tags = order.tags | split: ", " %}
{% unless order_tags contains value %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ order.admin_graphql_api_id | json }}
tags: {{ value | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endunless %}
{% endif %}
{% if options.tag_customer_with_parameter_value__boolean %}
{% assign customer_qualifies = true %}
{% assign customer_tags = order.customer.tags | split: ", " %}
{% if customer_tags contains value %}
{% assign customer_qualifies = false %}
{% endif %}
{% if options.only_tag_the_customer_for_their_first_order__boolean %}
{% comment %}
We go the long way about this, because (a) in a test store,
Shopify is not incrementing customer.orders_count, and (b)
it does not seem like the order reliably exists for retrieval
the instant it is placed. This way, we can mark the customer
as qualified if there are no orders, or if there is an order
and this is it.
{% endcomment %}
{% capture query %}
query {
customer(id: {{ order.customer.admin_graphql_api_id | json }}) {
tags
orders(first: 1) {
pageInfo {
hasNextPage
}
edges {
node {
id
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"customer": {
"tags": [],
"orders": {
"pageInfo": {
"hasNextPage": false
},
"edges": [
{
"node": {
"id": {{ order.admin_graphql_api_id | json }}
}
}
]
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% if result.data.customer.orders.pageInfo.hasNextPage %}
{% assign customer_qualifies = false %}
{% elsif result.data.customer.orders.edges != empty and result.data.customer.orders.edges.first.node.id != order.admin_graphql_api_id %}
{% assign customer_qualifies = false %}
{% endif %}
{% endif %}
{% if customer_qualifies and value != blank %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ order.customer.admin_graphql_api_id | json }}
tags: {{ value | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endif %}