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

Mechanic is a development 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 when a user triggers the task, when a product is created, and when a product is 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, 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.

Open source
View on GitHub to contribute to this task
Events
when a user triggers the task (mechanic/user/trigger)
when a product is created (shopify/products/create)
when a product is updated (shopify/products/update)
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)
Script
{% 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 %}

{% 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 %}
    {% action "shopify" %}
      [
        "update",
        [
          "product",
          {{ product.id | json }}
        ],
        {
          "metafields": [
            {
              {% if existing_metafield %}"id": {{ existing_metafield.id | json }},{% endif %}
              "namespace": "mechanic",
              "key": {{ metafield_key | json }},
              "value": {{ metafield_value_json | json }},
              "value_type": "json_string"
            }
          ]
        }
      ]
    {% endaction %}
  {% endif %}
{% endfor %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.