Auto-tag products when their variants change, with Mechanic.

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

Auto-tag products when their variants change

Need to print price tags whenever a SKU is added? Or make a note of a new variant price? Use this task to tag products that need your attention, whenever a specific variant attribute changes.

Runs Occurs when a user manually triggers the task, 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 variant attribute to watch for changes, tag product with the titles of each variant that has changed, product tag to add, and ignore products with any of these tags.

15-day free trial – unlimited tasks

Documentation

Need to print price tags whenever a SKU is added? Or make a note of a new variant price? Use this task to tag products that need your attention, whenever a specific variant attribute changes.

Configure this task with a specific variant attribute to watch for changes. Valid attributes include "barcode", "compare_at_price", "grams", "option1", "option2", "option3", "price", "sku", and "taxable".

Important: After saving this task, you must click the "Run task" button before the task will start monitoring your existing products. This task run will allow the task to scan your existing products, and store information about their existing variant attributes. This is what lets the task determine whether or not a specific variant attribute has changed.

Enable the "Tag with the titles of each variant that has changed" option to have this task add a separate tag for each variant that has had its specific attribute change values. The variant title will be appended to your configured tag, resulting in one or more tags per product, resembling "SKU RED" or "repriced 5 / SMALL", depending on your tag choice and variant options.

YouTube: Watch the development video!

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
mechanic/user/trigger
shopify/products/create
shopify/products/update
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
variant attribute to watch for changes (required), tag product with the titles of each variant that has changed (boolean), product tag to add (required), ignore products with any of these tags (array)
Code
{% comment %}
  An opinion about option order:

  {{ options.variant_attribute_to_watch_for_changes__required }}
  {{ options.tag_product_with_the_titles_of_each_variant_that_has_changed__boolean }}
  {{ options.product_tag_to_add__required }}
  {{ options.ignore_products_with_any_of_these_tags__array }}
{% endcomment %}

{% assign variant_attribute = options.variant_attribute_to_watch_for_changes__required %}

{% assign valid_variant_attributes = "barcode,compare_at_price,grams,option1,option2,option3,price,sku,taxable" | split: "," %}

{% unless valid_variant_attributes contains variant_attribute %}
  {% capture message -%}
    {{ variant_attribute | json }} is not a valid variant attribute. Choose one of these instead: {{ valid_variant_attributes | join: ", " }}
  {%- endcapture %}
  {% error message %}
{% endunless %}

{% assign metafield_key = "variant_ids_and_" | append: variant_attribute | append: "s" %}

{% assign products_that_need_a_metafield_update = array %}

{% if event.topic == "mechanic/user/trigger" %}
  {% if event.preview %}
    {% capture shop_json %}
      {
        "products": [
          {
            "variants": [
              {
                "id": 1234567890,
                {{ variant_attribute | json }}: "OHHELLO"
              },
              {
                "id": 2345678901,
                {{ variant_attribute | json }}: "OHGOODBYE"
              }
            ]
          }
        ]
      }
    {% endcapture %}

    {% assign shop = shop_json | parse_json %}
  {% endif %}

  {% assign products_that_need_a_metafield_update = shop.products %}
{% elsif event.topic == "shopify/products/create" %}
  {% assign products_that_need_a_metafield_update[0] = product %}
{% elsif event.topic == "shopify/products/update" %}
  {% if event.preview %}
    {% capture product_json %}
      {
        "admin_graphql_api_id": "gid://shopify/Product/12346567890",
        "variants": [
          {
            "id": 1234567890,
            {{ variant_attribute | json }}: "OHHELLO",
            "title": "OH / HELLO"
          },
          {
            "id": 2345678901,
            {{ variant_attribute | json }}: "OHGOODBYE",
            "title": "OH / GOODBYE"
          }
        ],
        "metafields": {
          "mechanic": {
            {{ metafield_key | json }}: {
              "1234567890": "OHHELLOOOOOOOOOOOOOOOOO",
              "2345678901": "OHGOODBYE"
            }
          }
        }
      }
    {% endcapture %}

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

  {% assign previous_variant_ids_and_values = product.metafields.mechanic[metafield_key] %}

  {% if previous_variant_ids_and_values == nil %}
    {% error %}
      "Mechanic has not had an opportunty to store this product's previous variant values. Use the \"Run task\" button to re-scan your products. :)"
    {% enderror %}
  {% endif %}

  {% assign product_qualifies = false %}

  {% assign tags_to_add = array %}

  {% unless options.tag_product_with_the_titles_of_each_variant_that_has_changed__boolean %}
    {% assign tags_to_add[0] = options.product_tag_to_add__required %}
  {% endunless %}

  {% for variant in product.variants %}
    {% assign variant_id_string = variant.id | append: "" %}
    {% if previous_variant_ids_and_values[variant_id_string] != variant[variant_attribute] %}
      {% assign product_qualifies = true %}

      {% if options.tag_product_with_the_titles_of_each_variant_that_has_changed__boolean %}
        {% if variant.title == "Default Title" %}
          {% assign tags_to_add[tags_to_add.size] = options.product_tag_to_add__required %}
        {% else %}
          {% assign tags_to_add[tags_to_add.size] = options.product_tag_to_add__required | append: " " | append: variant.title %}
        {% endif %}
      {% endif %}
    {% endif %}
  {% endfor %}

  {% if options.ignore_products_with_any_of_these_tags__array != empty %}
    {% assign product_tags = product.tags | split: ", " %}
    {% for product_tag in product_tags %}
      {% if options.ignore_products_with_any_of_these_tags__array contains product_tag %}
        {% log message: "Product is disqualified by tag", disqualifying_product_tag: product_tag %}
        {% assign product_qualifies = false %}
        {% break %}
      {% endif %}
    {% endfor %}
  {% endif %}

  {% if product_qualifies %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: {{ product.admin_graphql_api_id | json }}
          tags: {{ tags_to_add | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}

    {% assign products_that_need_a_metafield_update[0] = product %}
  {% endif %}
{% endif %}

{% assign metafields = array %}

{% for product in products_that_need_a_metafield_update %}
  {% assign metafield_value = hash %}
  {% assign existing_metafield = product.metafields.mechanic | where: "key", metafield_key | first %}

  {% for variant in product.variants %}
    {% assign variant_id_string = variant.id | append: "" %}
    {% assign metafield_value[variant_id_string] = variant[variant_attribute] %}
  {% endfor %}

  {% assign metafield_value_json = metafield_value | json %}

  {% if existing_metafield.value == metafield_value_json %}
    {% log message: "Product metafield is already up to date", product_id: product.id %}

  {% else %}
    {% capture metafield %}
      {
        ownerId: {{ product.admin_graphql_api_id | json }}
        namespace: "mechanic"
        key: {{ metafield_key | json }}
        value: {{ metafield_value_json | json }}
        type: "json"
      }
    {% endcapture %}

    {% assign metafields = metafields | push: metafield %}
  {% endif %}
{% endfor %}

{% assign groups_of_metafields = metafields | in_groups_of: 25, fill_with: false %}

{% for group_of_metafields in groups_of_metafields %}
  {% action "shopify" %}
    mutation {
      metafieldsSet(
        metafields: [
          {{ group_of_metafields | join: newline }}
        ]
      ) {
        metafields {
          id
          namespace
          key
          type
          value
          owner {
            ... on Product {
              id
            }
          }
        }
        userErrors {
          code
          field
          message
        }
      }
    }
  {% endaction %}
{% endfor %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more