-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-generate-skus.json
21 lines (21 loc) · 5.44 KB
/
auto-generate-skus.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"docs": "Save time by letting this task keep your variant SKUs up to date, generating them based on your product handle, and the initials of each variant option. Optionally, choose to have the task skip updating variants that already have SKUs. Useful for large catalogs, or for anyone who has a consistent SKU format.\n\nPlease note: This task updates SKUs for all products, whether or not they're configured with options and variants.\n\nThis task automatically maintains SKUs for your product variants, by combining these elements and joining them with a dash:\n\n1. The last portion of the product handle (e.g. `503`, if your product is available at `myshop.com/products/stylish-shirt-503`)\n2. The capital letters of the variant's first option, if there is one (e.g. `H` if the option is `Heather gray`, or `HG` if the option is `Heather Gray`)\n3. The capital letters of the variant's second option, if there is one\n4. The capital letters of the variant's third option, if there is one\n\n(To use the product option's full value, instead of abbreviating it, add the option name to the \"Product options to keep unabbreviated\" list.)\n\nTo illustrate, a shirt available at `myshop.com/products/stylish-shirt-503`, with options for size and color, might have these SKUs auto-generated:\n\n* Medium, Black: `503-M-B`\n* XL, Heather gray: `503-XL-H`\n* Small, Red: `503-S-R`\n\nTo update your product handle, so as to control the first portion of generated SKUs, open the product in the Shopify admin, scroll to the bottom of the page, click \"Edit website SEO\", and update the \"URL and handle\" field to taste. :)",
"halt_action_run_sequence_on_error": false,
"name": "Auto-generate SKUs",
"online_store_javascript": null,
"options": {
"skip_variants_that_already_have_skus__boolean": true,
"product_options_to_keep_unabbreviated__array": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% if event.preview %}\n {% capture product_json %}\n {\n \"handle\": \"stylish-shirt-503\",\n \"variants\": [\n {\n \"sku\": \"503-L-B\",\n \"option1\": \"L\",\n \"option2\": \"Black\",\n \"admin_graphql_api_id\": \"gid://shopify/ProductVariant/1234567890\"\n },\n {\n \"sku\": \"503-XL-G\",\n \"option1\": \"XL\",\n \"option2\": \"Heather Grey\",\n \"admin_graphql_api_id\": \"gid://shopify/ProductVariant/2345678901\"\n {% if options.skip_variants_that_already_have_skus__boolean %}\n },\n {\n \"option1\": \"XXL\",\n \"option2\": \"Red\",\n \"admin_graphql_api_id\": \"gid://shopify/ProductVariant/3456789012\"\n {% endif %}\n }\n ],\n \"options\": [\n {\n \"name\": \"Size\"\n },\n {\n \"name\": \"Color\"\n }\n ]\n }\n {% endcapture %}\n\n {% assign product = product_json | parse_json %}\n{% endif %}\n\n{% assign allowed_characters = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\" %}\n\n{% assign sku_base = product.handle | split: \"-\" | last | upcase %}\n\n{% for variant in product.variants %}\n {% if options.skip_variants_that_already_have_skus__boolean and variant.sku != blank %}\n {% log message: \"variant already has a sku; skipping\", original_sku: variant.sku %}\n {% continue %}\n {% endif %}\n\n {% assign variant_sku_generated = sku_base %}\n\n {% assign variant_options = array %}\n {% assign variant_options[0] = variant.option1 %}\n {% assign variant_options[1] = variant.option2 %}\n {% assign variant_options[2] = variant.option3 %}\n\n {% assign variant_option_names = product.options | map: \"name\" %}\n\n {% for variant_option in variant_options %}\n {% if variant_option == blank %}\n {% continue %}\n {% endif %}\n\n {% if options.product_options_to_keep_unabbreviated__array contains variant_option_names[forloop.index0] %}\n {% assign variant_sku_generated = variant_sku_generated | append: \"-\" | append: variant_option %}\n {% else %}\n {% assign variant_option_abbreviation = \"\" %}\n {% assign variant_option_characters = variant_option | split: \"\" %}\n {% for variant_option_character in variant_option_characters %}\n {% if allowed_characters contains variant_option_character %}\n {% assign variant_option_abbreviation = variant_option_abbreviation | append: variant_option_character %}\n {% endif %}\n {% endfor %}\n\n {% if variant_option_abbreviation == blank %}\n {% assign variant_option_abbreviation = variant_option_characters[0] | upcase %}\n {% endif %}\n\n {% assign variant_sku_generated = variant_sku_generated | append: \"-\" | append: variant_option_abbreviation %}\n {% endif %}\n {% endfor %}\n\n {% if variant_sku_generated != variant.sku %}\n {% log message: \"updating sku\", original_sku: variant.sku, generated_sku: variant_sku_generated %}\n\n {% action \"shopify\" %}\n mutation {\n productVariantUpdate(input: {\n id: {{ variant.admin_graphql_api_id | json }}\n sku: {{ variant_sku_generated | json }}\n }) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n{% endfor %}",
"subscriptions": [
"shopify/products/create",
"shopify/products/update"
],
"subscriptions_template": "shopify/products/create\nshopify/products/update",
"tags": [
"SKU"
]
}