-
Notifications
You must be signed in to change notification settings - Fork 35
/
script.liquid
81 lines (73 loc) · 2.56 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
{% assign existing_metafield = fulfillment.order.metafields.mechanic | where: "key", "known_tracking_numbers" | first %}
{% if event.preview %}
{% capture fulfillment_json %}
{
"order": {
"email": "[email protected]",
"metafields": {
"mechanic": {
"known_tracking_numbers": []
}
},
"fulfillments": [
{
"tracking_numbers": ["ABC123"]
}
]
}
}
{% endcapture %}
{% assign fulfillment = fulfillment_json | parse_json %}
{% assign existing_metafield = hash %}
{% assign existing_metafield["id"] = 1234567890 %}
{% endif %}
{% assign already_known_tracking_numbers = fulfillment.order.metafields.mechanic.known_tracking_numbers | default: array %}
{% assign currently_known_tracking_numbers = array %}
{% for some_fulfillment in fulfillment.order.fulfillments %}
{% assign currently_known_tracking_numbers = currently_known_tracking_numbers | concat: some_fulfillment.tracking_numbers %}
{% endfor %}
{% assign tracking_numbers_changed = false %}
{% for tracking_number in currently_known_tracking_numbers %}
{% unless already_known_tracking_numbers contains tracking_number %}
{% assign tracking_numbers_changed = true %}
{% break %}
{% endunless %}
{% endfor %}
{% if tracking_numbers_changed or existing_metafield == nil %}
{% if existing_metafield %}
{% if fulfillment.order.email == blank %}
{% log "No email on file for this order." %}
{% else %}
{% assign tracking_numbers_string = currently_known_tracking_numbers | join: ", " %}
{% action "email" %}
{
"to": {{ fulfillment.order.email | json }},
"subject": {{ options.email_subject__required | json }},
"body": {{ options.email_body__required_multiline | replace: "TRACKING_NUMBERS", tracking_numbers_string | strip | newline_to_br | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}
{% endif %}
{% endif %}
{% action "shopify" %}
[
"update",
[
"order",
{{ fulfillment.order.id | json }}
],
{
"metafields": [
{
{% if existing_metafield %}"id": {{ existing_metafield.id | json }},{% endif %}
"namespace": "mechanic",
"key": "known_tracking_numbers",
"value": {{ currently_known_tracking_numbers | json | json }},
"type": "json"
}
]
}
]
{% endaction %}
{% endif %}