-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathscript.liquid
99 lines (88 loc) · 4.45 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
{% comment %}
Preferred option order:
{{ options.order_tag_to_watch_for__required }}
{{ options.country_codes_and_tracking_url_templates__keyval }}
{{ options.default_tracking_url_template }}
{{ options.overwrite_existing_tracking_urls__boolean }}
{{ options.country_codes_and_tracking_companies__keyval }}
{{ options.default_tracking_company }}
{{ options.notify_customers__boolean }}
{% endcomment %}
{% if options.country_codes_and_tracking_url_templates__keyval == blank and options.default_tracking_url_template == blank %}
{% error "Fill in at least one of 'Country codes and tracking URL templates' and 'Fallback tracking URL template'. :)" %}
{% endif %}
{% for pair in options.country_codes_and_tracking_url_templates__keyval %}
{% unless pair[1] contains "TRACKING_NUMBER" %}
{% error %}{{ "The template for " | append: pair[0] | append: " does not contain the placeholder 'TRACKING_NUMBER'. Please add it!" | json }}{% enderror %}
{% endunless %}
{% endfor %}
{% if options.default_tracking_url_template != blank %}
{% unless options.default_tracking_url_template contains "TRACKING_NUMBER" %}
{% error %}{{ "The default template for does not contain the placeholder 'TRACKING_NUMBER'. Please add it!" | json }}{% enderror %}
{% endunless %}
{% endif %}
{% if event.preview %}
{% capture order_json %}
{
"tags": {{ options.order_tag_to_watch_for__required | json }},
"shipping_address": {
"country_code": {{ options.country_codes_and_tracking_url_templates__keyval.first.first | json }}
},
"fulfillments": [
{
"admin_graphql_api_id": "gid://shopify/Fulfillment/1234567890",
"name": "#1234.1",
"tracking_company": "Other",
"tracking_number": "ABCDEF123456",
"tracking_url": null
}
]
}
{% endcapture %}
{% assign order = order_json | parse_json %}
{% endif %}
{% assign order_tags = order.tags | split: ", " %}
{% if order_tags contains options.order_tag_to_watch_for__required %}
{% assign template = options.country_codes_and_tracking_url_templates__keyval[order.shipping_address.country_code] %}
{% assign template = template | default: options.default_tracking_url_template %}
{% for fulfillment in order.fulfillments %}
{% if fulfillment.tracking_number == blank or fulfillment.tracking_number == "false" %}
{% continue %}
{% endif %}
{% assign new_fulfillment_tracking_url = template | replace: "TRACKING_NUMBER", fulfillment.tracking_number %}
{% assign new_fulfillment_tracking_company = options.country_codes_and_tracking_companies__keyval[order.shipping_address.country_code] | default: options.default_tracking_company | default: fulfillment.tracking_company %}
{% if options.overwrite_existing_tracking_urls__boolean == false and fulfillment.tracking_url != blank %}
{% log message: "Fulfillment already has a tracking URL, and this task is configured not to overwrite, so we're sticking with the existing tracking URL", new_fulfillment_tracking_url: new_fulfillment_tracking_url, fulfillment_tracking_url: fulfillment.tracking_url, fulfillment_name: fulfillment.name, fulfillment_id: fulfillment.id %}
{% assign new_fulfillment_tracking_url = fulfillment.tracking_url %}
{% endif %}
{% if new_fulfillment_tracking_url == fulfillment.tracking_url and new_fulfillment_tracking_company == fulfillment.tracking_company %}
{% log message: "Tracking URL and company is unchanged; skipping this fulfillment", fulfillment_tracking_company: fulfillment.tracking_company, fulfillment_tracking_url: fulfillment.tracking_url, fulfillment_name: fulfillment.name, fulfillment_id: fulfillment.id %}
{% else %}
{% action "shopify" %}
mutation {
fulfillmentTrackingInfoUpdateV2(
fulfillmentId: {{ fulfillment.admin_graphql_api_id | json }}
trackingInfoInput: {
company: {{ new_fulfillment_tracking_company | json }}
number: {{ fulfillment.tracking_number | json }}
url: {{ new_fulfillment_tracking_url | json }}
}
notifyCustomer: {{ options.notify_customers__boolean | json }}
) {
fulfillment {
trackingInfo {
company
number
url
}
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endfor %}
{% endif %}