forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.liquid
103 lines (96 loc) · 3.26 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
{% if options.order_attribute_name_for_storing_the_draft_order_id == blank and options.order_attribute_name_for_storing_the_draft_order_name == blank %}
{% error "Please fill in at least one order attribute name." %}
{% endif %}
{% if event.preview %}
{% assign order = hash %}
{% assign order["admin_graphql_api_id"] = "gid://shopify/Order/1234567890" %}
{% assign order["note_attributes"] = array %}
{% assign order["note_attributes"][0] = hash %}
{% assign order["note_attributes"][0]["name"] = "Existing attribute name" %}
{% assign order["note_attributes"][0]["value"] = "Existing attribute value" %}
{% endif %}
{% capture query %}
query {
order(id: {{ order.admin_graphql_api_id | json }}) {
events(first: 1, sortKey: CREATED_AT) {
edges {
node {
message
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"order": {
"events": {
"edges": [
{
"node": {
"message": "Staff Member created this order from draft order <a href=\"https://example.myshopify.com/admin/draft_orders/1234567890\">#1000</a>."
}
}
]
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign event_message = result.data.order.events.edges.first.node.message %}
{% if event_message contains ".myshopify.com/admin/draft_orders/" %}
{% assign draft_order_id = event_message | split: "/draft_orders/" | last | split: '"' | first %}
{% assign draft_order_name = event_message | split: ">#" | last | split: "</a>" | first | prepend: "#" %}
{% if draft_order_id == blank %}
{% log message: "Couldn't locate the draft order ID", event_message: event_message %}
{% else %}
{% action "shopify" %}
mutation {
orderUpdate(
input: {
id: {{ order.admin_graphql_api_id | json }}
customAttributes: [
{% for note_attribute in order.note_attributes %}
{
key: {{ note_attribute.name | json }}
value: {{ note_attribute.value | json }}
}
{% endfor %}
{% if options.order_attribute_name_for_storing_the_draft_order_id != blank %}
{
key: {{ options.order_attribute_name_for_storing_the_draft_order_id | json }}
value: {{ draft_order_id | json }}
}
{% endif %}
{% if options.order_attribute_name_for_storing_the_draft_order_name != blank %}
{
key: {{ options.order_attribute_name_for_storing_the_draft_order_name | json }}
value: {{ draft_order_name | json }}
}
{% endif %}
]
}
) {
order {
customAttributes {
key
value
}
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% else %}
{% log message: "Event message didn't mention a draft order", event_message: event_message %}
{% endif %}