Keep SKUs up to date with barcodes, with Mechanic.

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

Keep SKUs up to date with barcodes

This task watches for new and updated products, copying variant barcodes over to the variants' inventory item SKUs. This occurs whenever a barcode is found, and the related SKU does not already have that value.

Runs Occurs whenever a product is created, Occurs whenever a product is updated, ordered, or variants are added, removed or updated, Occurs when a user manually triggers the task, and Occurs when a bulk operation is completed.

15-day free trial – unlimited tasks

Documentation

This task watches for new and updated products, copying variant barcodes over to the variants' inventory item SKUs. This occurs whenever a barcode is found, and the related SKU does not already have that value.

The task may also be run manually to scan all variants and inventory items in the shop, updating the unmatched SKUs as needed.

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
mechanic/user/trigger
mechanic/shopify/bulk_operation
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Code
{% if event.topic == "shopify/products/create" or event.topic == "shopify/products/update" %}
  {% comment %}
    -- query all variants for this product, along with their inventory items
  {% endcomment %}

  {% capture query %}
    query {
      product(id: {{ product.admin_graphql_api_id | json }}) {
        variants(first: 100) {
          nodes {
            id
            barcode
            sku
            inventoryItem {
              id
            }
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% assign variants = result.data.product.variants.nodes %}

{% elsif event.topic == "mechanic/user/trigger" %}
  {% comment %}
    -- use bulk op to query all variants in the shop, along with their inventory items
  {% endcomment %}

  {% capture bulk_operation_query %}
    query {
      productVariants {
        edges {
          node {
            __typename
            id
            barcode
            sku
            inventoryItem {
              id
            }
          }
        }
      }
    }
  {% endcapture %}

  {% action "shopify" %}
    mutation {
      bulkOperationRunQuery(
        query: {{ bulk_operation_query | json }}
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}

{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
  {% assign variants = bulkOperation.objects %}
{% endif %}

{% if event.preview %}
  {% capture variants_json %}
    [
      {
        "id": "gid://shopify/ProductVariant/1234567890",
        "barcode": "ABC123",
        "sku": "",
        "inventoryItem": {
          "id": "gid://shopify/InventoryItem/1234567890"
        }
      }
    ]
  {% endcapture %}

  {% assign variants = variants_json | parse_json %}
{% endif %}

{% comment %}
  -- update skus on inventory items, as this is no longer supported on variants as of 2024-07 API
  -- note: unlike variants, there is not yet a bulk update mutation for inventory items
{% endcomment %}

{% for variant in variants %}
  {% if variant.barcode != blank and variant.sku != variant.barcode %}
    {% action "shopify" %}
      mutation {
        inventoryItemUpdate(
          id: {{ variant.inventoryItem.id | json }}
          input: {
            sku: {{ variant.barcode | json }}
          }
        ) {
          inventoryItem {
            variant {
              id
              displayName
              barcode
              sku
            }
          }
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endfor %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more