-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathscript.liquid
178 lines (154 loc) · 4.72 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
170
171
172
173
174
175
176
177
178
{% assign metafields_and_tag_prefixes = options.metafields_and_tag_prefixes__keyval_required %}
{% assign truncate_long_tags = options.truncate_long_tags__boolean %}
{% assign metafields = metafields_and_tag_prefixes | keys %}
{% assign supported_metafield_types
= array
| push:
"boolean",
"date",
"date_time",
"number_decimal",
"number_integer",
"single_line_text_field"
%}
{% if event.topic == "shopify/orders/create" or event.topic == "mechanic/user/order" %}
{% capture query %}
query {
order(id: {{ order.admin_graphql_api_id | json }}) {
id
name
tags
purchasingEntity {
__typename
... on PurchasingCompany {
company {
metafields(
first: 250
keys: {{ metafields | graphql_arguments }}
) {
nodes {
key
type
value
}
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"order": {
"id": "gid://shopify/Order/1234567890",
"name": "#PREVIEW",
"purchasingEntity": {
"__typename": "PurchasingCompany",
"company": {
"metafields": {
"nodes": [
{
"key": {{ metafields.first | json }},
"type": "list.single_line_text_field",
"value": "[\"Preview value\"]"
}
]
}
}
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign order = result.data.order %}
{% if order.purchasingEntity.__typename != "PurchasingCompany" %}
{% log "This order is not a company purchase; skipping." %}
{% break %}
{% endif %}
{% comment %}
-- loop through any of the configured metafields that were found on the company for this order
{% endcomment %}
{% assign tags_to_add = array %}
{% for metafield in order.purchasingEntity.company.metafields.nodes %}
{% assign tag_prefix = metafields_and_tag_prefixes[metafield.key] %}
{% assign metafield_type = metafield.type | remove: "list." %}
{% unless supported_metafield_types contains metafield_type %}
{% log
message: "Unsupported metafield type for this task.",
metafield: metafield,
supported_metafield_types: supported_metafield_types
%}
{% continue %}
{% endunless %}
{% comment %}
-- convert all values to an array so list metafield types can more easily be supported
{% endcomment %}
{% if metafield.type contains "list." %}
{% assign metafield_values = metafield.value | parse_json %}
{% else %}
{% assign metafield_values = array | push: metafield.value %}
{% endif %}
{% comment %}
-- determine tags by metafield value, if there is a prefix tag configured, and length
{% endcomment %}
{% for metafield_value in metafield_values %}
{% assign tag = metafield_value | prepend: tag_prefix %}
{% if tag.size > 40 %}
{% comment %}
-- order tags only support 40 characters max
{% endcomment %}
{% if truncate_long_tags %}
{% assign truncated_tag = tag | slice: 0, 40 %}
{% log
message: "Tag is too long to be set on the order, and tag truncation is enabled; applying to this tag.",
original_tag: tag,
truncated_tag: truncated_tag
%}
{% assign tag = truncated_tag %}
{% else %}
{% log
message: "Tag is too long to be set on the order, and tag truncation is not enabled; skipping this tag,",
tag: tag,
tag_length: tag.size
%}
{% continue %}
{% endif %}
{% endif %}
{% unless order.tags contains tag %}
{% assign tags_to_add = tags_to_add | push: tag %}
{% endunless %}
{% endfor %}
{% endfor %}
{% comment %}
-- add tags to the order as needed
{% endcomment %}
{% if tags_to_add != blank %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ order.id | json }}
tags: {{ tags_to_add | json }}
) {
node {
... on Order {
name
tags
}
}
userErrors {
field
message
}
}
}
{% endaction %}
{% else %}
{% log "No tagging operations needed for this order." %}
{% endif %}
{% endif %}