Auto-generate SKUs with Mechanic.

Mechanic is the one-tool-does-it-all automation app for Shopify. :)

Auto-generate SKUs

by Isaac Bowen (team@usemechanic.com)

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. Useful for large catalogs, or for anyone who has a consistent SKU format. Like all tasks, this one is easy to update for your business's needs - get in touch if you need a hand.

Runs when a product is created and when a product is updated.

15-day free trial – unlimited tasks

Documentation

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 (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

For example, 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, Gurus, 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.

Events
when a product is created (shopify/products/create)
when a product is updated (shopify/products/update)
Script
{% if event.preview %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "update",
        ["product", 1234567890],
        {
          "variants": [
            {
              "id": 1234567890,
              "sku": "503-XL-H"
            }
          ]
        }
      ]
    }
  }
{% elsif product.variants.size == 1 %}
  {"log": "The product only has one variant, and therefore has no product options from which a SKU can be generated; skipping update"}
{% else %}
  {% assign all_caps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" %}

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

  {% assign variant_updates = "" %}

  {% for variant in product.variants %}
    {% assign variant_sku_generated = sku_base %}
    {% assign variant_options = variant.option1 | append: "~~~" | append: variant.option2 | append: "~~~" | append: variant.option3 | split: "~~~" %}

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

      {% assign variant_option_abbreviation = "" %}
      {% assign variant_option_characters = variant_option | split: "" %}
      {% for variant_option_character in variant_option_characters %}
        {% if all_caps 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 %}
    {% endfor %}

    {% if variant_sku_generated != variant.sku %}
      {% capture variant_updates %}
        {% if variant_updates != blank %}
          {{ variant_updates }},
        {% endif %}

        {
          "id": {{ variant.id | json }},
          "sku": {{ variant_sku_generated | json }}
        }
      {% endcapture %}
    {% endif %}
  {% endfor %}

  {% if variant_updates != blank %}
    {
      "action": {
        "type": "shopify",
        "options": [
          "update",
          ["product", {{ product.id | json }}],
          {
            "variants": [{{ variant_updates }}]
          }
        ]
      }
    }
  {% else %}
    {"log": "All variants have the correct SKU; skipping update."}
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.