-
Notifications
You must be signed in to change notification settings - Fork 35
/
script.liquid
122 lines (109 loc) · 3.48 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
{% if event.preview %}
{% assign product = hash %}
{% assign product["admin_graphql_api_id"] = "gid://shopify/Product/1234567890" %}
{% endif %}
{% assign publication_ids = array %}
{% capture publication_query %}
query {
publications(first: 250) {
edges {
node {
id
name
}
}
}
}
{% endcapture %}
{% assign publication_result = publication_query | shopify %}
{% if event.preview %}
{% capture publication_result_json %}
{
"data": {
"publications": {
"edges": [
{% for name in options.sales_channel_names__array_required %}
{
"node": {
"id": "gid://shopify/Publication/1234567890",
"name": {{ name | json }}
}
}
{% unless forloop.last %}
,
{% endunless %}
{% endfor %}
]
}
}
}
{% endcapture %}
{% assign publication_result = publication_result_json | parse_json %}
{% endif %}
{% for publication_edge in publication_result.data.publications.edges %}
{% if options.sales_channel_names__array_required contains publication_edge.node.name %}
{% assign publication_ids[publication_ids.size] = publication_edge.node.id %}
{% endif %}
{% endfor %}
{% if publication_ids.size != options.sales_channel_names__array_required.size %}
{% error %}
{{ "Only found " | append: publication_ids.size | append: " existing sales channels - check your task configuration, and fix any incorrect names. :)" | json }}
{% enderror %}
{% else %}
{% capture product_query %}
query {
product(id: {{ product.admin_graphql_api_id | json }}) {
{% for publication_id in publication_ids %}
publishedOnPublication{{ forloop.index }}: publishedOnPublication(publicationId: {{ publication_id | json }})
{% endfor %}
}
}
{% endcapture %}
{% assign product_result = product_query | shopify %}
{% if event.preview %}
{% capture product_result_json %}
{
"data": {
"product": {
{% for publication_id in publication_ids %}
"publishedOnPublication{{ forloop.index }}": false
{% unless forloop.last %}
,
{% endunless %}
{% endfor %}
}
}
}
{% endcapture %}
{% assign product_result = product_result_json | parse_json %}
{% endif %}
{% assign publication_ids_for_publishing = array %}
{% for publication_id in publication_ids %}
{% assign key = "publishedOnPublication" | append: forloop.index %}
{% assign published = product_result.data.product[key] %}
{% unless published %}
{% assign publication_ids_for_publishing[publication_ids_for_publishing.size] = publication_id %}
{% endunless %}
{% endfor %}
{% if publication_ids_for_publishing == empty %}
{% log "Product was already published on all configured sales channel(s). :)" %}
{% else %}
{% action "shopify" %}
mutation {
{% for publication_id in publication_ids_for_publishing %}
publishablePublish{{ forloop.index }}: publishablePublish(
id: {{ product.admin_graphql_api_id | json }}
input: {
publicationId: {{ publication_id | json }}
}
) {
userErrors {
field
message
}
}
{% endfor %}
}
{% endaction %}
{% endif %}
{% endif %}