forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.liquid
97 lines (89 loc) · 2.18 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
{% assign customer_metafields_and_order_attributes = options.customer_metafields_and_order_attributes__keyval_required %}
{% capture query %}
query {
order(id: {{ order.admin_graphql_api_id | json }}) {
id
customer {
metafields(
first: {{ customer_metafields_and_order_attributes.size }}
keys: {{ customer_metafields_and_order_attributes | keys | json }}
) {
nodes {
key
value
}
}
}
customAttributes {
key
value
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"order": {
"id": "gid://shopify/Order/1234567890",
"customer": {
"metafields": {
"nodes": [
{
"key": {{ customer_metafields_and_order_attributes | first | first | json }},
"value": "Alpha"
}
]
}
},
"customAttributes": [
{
"key": "__sample",
"value": "Beta"
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign metafields = result.data.order.customer.metafields.nodes %}
{% assign attributes = result.data.order.customAttributes | default: array %}
{% log
existing_attributes_on_order: attributes,
matched_customer_metafields: metafields
%}
{% for metafield in metafields %}
{% assign attribute = hash %}
{% assign attribute["key"] = customer_metafields_and_order_attributes[metafield.key] %}
{% assign attribute["value"] = metafield.value %}
{% assign attributes = attributes | push: attribute %}
{% else %}
{% break %}
{% endfor %}
{% action "shopify" %}
mutation {
orderUpdate(
input: {
id: {{ order.admin_graphql_api_id | json }}
customAttributes: {{ attributes | graphql_arguments }}
}
) {
order {
id
name
customAttributes {
key
value
}
}
userErrors {
field
message
}
}
}
{% endaction %}