-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathscript.liquid
155 lines (135 loc) · 4.2 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
{% assign skip_variants_that_already_have_skus = options.skip_variants_that_already_have_skus__boolean %}
{% assign product_options_to_keep_unabbreviated = options.product_options_to_keep_unabbreviated__array %}
{% assign allowed_characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" %}
{% assign sku_base = product.handle | split: "-" | last | upcase %}
{% if event.preview %}
{% assign sku_base = "503" %}
{% endif %}
{% assign cursor = nil %}
{% assign variants = array %}
{% for n in (1..8) %}
{% capture query %}
query {
product(id: {{ product.admin_graphql_api_id | json }}) {
id
handle
variants(
first: 250
after: {{ cursor | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
inventoryItem {
id
}
sku
selectedOptions {
name
value
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"product": {
"variants": {
"nodes": [
{
"inventoryItem": {
"id": "gid://shopify/InventoryItem/1234567890"
},
{% unless skip_variants_that_already_have_skus %}"sku": "503-XL-G",{% endunless %}
"selectedOptions": [
{
"name": "Size",
"value": "XL"
},
{
"name": "Color",
"value": "Heather Grey"
}
]
}
]
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign variants = variants | concat: result.data.product.variants.nodes %}
{% if result.data.product.variants.pageInfo.hasNextPage %}
{% assign cursor = result.data.product.variants.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% for variant in variants %}
{% if skip_variants_that_already_have_skus and variant.sku != blank %}
{% log
message: "variant already has a sku; skipping",
original_sku: variant.sku
%}
{% continue %}
{% endif %}
{% assign variant_sku_generated = sku_base %}
{% for selected_option in variant.selectedOptions %}
{% if product_options_to_keep_unabbreviated contains selected_option.name %}
{% assign variant_sku_generated = variant_sku_generated | append: "-" | append: selected_option.value %}
{% else %}
{% assign variant_option_abbreviation = "" %}
{% assign variant_option_characters = selected_option.value | split: "" %}
{% for variant_option_character in variant_option_characters %}
{% if allowed_characters contains variant_option_character %}
{% assign variant_option_abbreviation = variant_option_abbreviation | append: variant_option_character %}
{% endif %}
{% endfor %}
{% if variant_option_abbreviation == blank %}
{% assign variant_option_abbreviation = variant_option_characters[0] | upcase %}
{% endif %}
{% assign variant_sku_generated = variant_sku_generated | append: "-" | append: variant_option_abbreviation %}
{% endif %}
{% endfor %}
{% if variant_sku_generated != variant.sku %}
{% log
message: "updating sku",
original_sku: variant.sku,
generated_sku: variant_sku_generated
%}
{% comment %}
-- as of the 2024-07 API, SKUs are updated on inventory items instead of variants
{% endcomment %}
{% action "shopify" %}
mutation {
inventoryItemUpdate(
id: {{ variant.inventoryItem.id | json }}
input: {
sku: {{ variant_sku_generated | json }}
}
) {
inventoryItem {
variant {
id
displayName
sku
}
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endfor %}