Auto-generate SKUs, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

Auto-generate SKUs

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.

Runs Occurs whenever a product is created and Occurs whenever a product is updated, or whenever a product is ordered, or whenever a variant is added, removed, or updated. Configuration includes skip variants that already have skus and product options to keep unabbreviated.

15-day free trial – unlimited tasks

Documentation

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.

Please note: This task updates SKUs for all products, whether or not they're configured with options and variants.

This task automatically maintains SKUs for your product variants, by combining these elements and joining them with a dash:

  1. The last portion of the product handle (e.g. 503, if your product is available at myshop.com/products/stylish-shirt-503)
  2. 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)
  3. The capital letters of the variant's second option, if there is one
  4. The capital letters of the variant's third option, if there is one

(To use the product option's full value, instead of abbreviating it, add the option name to the "Product options to keep unabbreviated" list.)

To illustrate, a shirt available at myshop.com/products/stylish-shirt-503, with options for size and color, might have these SKUs auto-generated:

  • Medium, Black: 503-M-B
  • XL, Heather gray: 503-XL-H
  • Small, Red: 503-S-R

To 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. :)

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, everybody.

That’s why we make it easy to configure automation without code, why we make it easy to tweak the underlying code once tasks are installed, and why we publish it all here for everyone to learn from.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
shopify/products/create
shopify/products/update
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
skip variants that already have skus (boolean), product options to keep unabbreviated (array)
Code
{% if event.preview %}
  {% capture product_json %}
    {
      "handle": "stylish-shirt-503",
      "variants": [
        {
          "sku": "503-L-B",
          "option1": "L",
          "option2": "Black",
          "admin_graphql_api_id": "gid://shopify/ProductVariant/1234567890"
        },
        {
          "sku": "503-XL-G",
          "option1": "XL",
          "option2": "Heather Grey",
          "admin_graphql_api_id": "gid://shopify/ProductVariant/2345678901"
      {% if options.skip_variants_that_already_have_skus__boolean %}
        },
        {
          "option1": "XXL",
          "option2": "Red",
          "admin_graphql_api_id": "gid://shopify/ProductVariant/3456789012"
      {% endif %}
        }
      ],
      "options": [
        {
          "name": "Size"
        },
        {
          "name": "Color"
        }
      ]
    }
  {% endcapture %}

  {% assign product = product_json | parse_json %}
{% endif %}

{% assign allowed_characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" %}

{% assign sku_base = product.handle | split: "-" | last | upcase %}

{% for variant in product.variants %}
  {% if options.skip_variants_that_already_have_skus__boolean and variant.sku != blank %}
    {% log message: "variant already has a sku; skipping", original_sku: variant.sku %}
    {% continue %}
  {% endif %}

  {% assign variant_sku_generated = sku_base %}

  {% assign variant_options = array %}
  {% assign variant_options[0] = variant.option1 %}
  {% assign variant_options[1] = variant.option2 %}
  {% assign variant_options[2] = variant.option3 %}

  {% assign variant_option_names = product.options | map: "name" %}

  {% for variant_option in variant_options %}
    {% if variant_option == blank %}
      {% continue %}
    {% endif %}

    {% if options.product_options_to_keep_unabbreviated__array contains variant_option_names[forloop.index0] %}
      {% assign variant_sku_generated = variant_sku_generated | append: "-" | append: variant_option %}
    {% else %}
      {% assign variant_option_abbreviation = "" %}
      {% assign variant_option_characters = variant_option | 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 %}

    {% action "shopify" %}
      mutation {
        productVariantUpdate(input: {
          id: {{ variant.admin_graphql_api_id | json }}
          sku: {{ variant_sku_generated | json }}
        }) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endfor %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Skip variants that already have SKUs
true