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 variant SKU. This occurs whenever a barcode is found, and the related variant's SKU does not already have that value.

Runs Occurs whenever a product is created, Occurs whenever a product is deleted, 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 variant SKU. This occurs whenever a barcode is found, and the related variant's SKU does not already have that value.

The task may also be run manually to scan all products and variants 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/delete
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" %}
  {% if event.preview %}
    {% capture product_json %}
      {
        "variants": [
          {
            "admin_graphql_api_id": "gid://shopify/ProductVariant/1234567890",
            "barcode": "ABC123",
            "sku": ""
          }
        ]
      }
    {% endcapture %}

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

  {% for variant in product.variants %}
    {% if variant.barcode != blank and variant.sku != variant.barcode %}
      {% action "shopify" %}
        mutation {
          productVariantUpdate(
            input: {
              id: {{ variant.admin_graphql_api_id | json }}
              sku: {{ variant.barcode | json }}
            }
          ) {
            productVariant {
              barcode
              sku
            }
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}
  {% endfor %}

{% elsif event.topic == "mechanic/user/trigger" %}
  {% capture bulk_operation_query %}
    query {
      products {
        edges {
          node {
            __typename
            id
            variants {
              edges {
                node {
                  __typename
                  id
                  barcode
                  sku
                }
              }
            }
          }
        }
      }
    }
  {% endcapture %}

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

{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
  {% if event.preview %}
    {% capture jsonl_string %}
      {"__typename":"Product","id":"gid:\/\/shopify\/Product\/1234567890"}
      {"__typename":"ProductVariant","id":"gid:\/\/shopify\/ProductVariant\/1234567890","barcode":"123ABC","__parentId":"gid:\/\/shopify\/Product\/1234567890"}
      {"__typename":"ProductVariant","id":"gid:\/\/shopify\/ProductVariant\/2345678901","barcode":"456DEF","__parentId":"gid:\/\/shopify\/Product\/1234567890"}
    {% endcapture %}

    {% assign bulkOperation = hash %}
    {% assign bulkOperation["objects"] = jsonl_string | parse_jsonl %}
  {% endif %}

  {% assign products = bulkOperation.objects | where: "__typename", "Product" %}
  {% assign bulk_variants = bulkOperation.objects | where: "__typename", "ProductVariant" %}

  {% for product in products %}
    {% assign variants = bulk_variants | where: "__parentId", product.id %}

    {% assign variant_inputs = array %}

    {% for variant in variants %}
      {% if variant.barcode != blank and variant.barcode != variant.sku %}
        {% assign variant_input = hash %}
        {% assign variant_input["id"] = variant.id %}
        {% assign variant_input["sku"] = variant.barcode %}
        {% assign variant_inputs = variant_inputs | push: variant_input %}
      {% endif %}
    {% endfor %}

    {% if variant_inputs != blank %}
      {% action "shopify" %}
        mutation {
          productVariantsBulkUpdate(
            allowPartialUpdates: true
            productId: {{ product.id | json }}
            variants: {{ variant_inputs | graphql_arguments }}
          ) {
            product {
              title
            }
            productVariants {
              displayName
              barcode
              sku
            }
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}
  {% endfor %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more