Auto-tag products by their options, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Auto-tag products by their options

by Isaac Bowen (team@usemechanic.com)

Use this task to add tags to your products, based on their options, for easy filtering. For example, use this task to tag your shirts with "Color-Blue" and "Size-XL", allowing products to be easily located by what options they offer.

Runs when a user triggers the task, when a product is created, and when a product is updated. Configuration includes product options to consider and run for products when they are created or updated.

15-day free trial – unlimited tasks

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 user triggers the task (mechanic/user/trigger)
when a product is created (shopify/products/create)
when a product is updated (shopify/products/update)
Options
product options to consider (array, required), run for products when they are created or updated (boolean)
Script
{% if event.preview %}
  {% action "shopify" %}
    mutation {
      tagsAdd(
        id: "gid://shopify/Product/1234567890"
        tags: {{ options.product_options_to_consider__array_required.first | append: "-Blue" | json }}
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% else %}
  {% assign products = nil %}

  {% if event.topic contains "shopify/products/" %}
    {% assign products = array %}
    {% assign products[0] = product %}
  {% elsif event.topic == "mechanic/user/trigger" %}
    {% assign products = shop.products %}
  {% endif %}

  {% for product in products %}
    {% assign product_tags = product.tags | split: ", " %}
    {% assign product_tags_applicable = array %}
    {% assign product_tags_to_add = array %}
    {% assign product_tags_to_remove = array %}

    {% for variant in product.variants %}
      {% if options.product_options_to_consider__array_required contains product.options[0].name %}
        {% assign tag = product.options[0].name | append: "-" | append: variant.option1 %}
        {% assign product_tags_applicable[product_tags_applicable.size] = tag %}
      {% endif %}

      {% if options.product_options_to_consider__array_required contains product.options[1].name %}
        {% assign tag = product.options[1].name | append: "-" | append: variant.option2 %}
        {% assign product_tags_applicable[product_tags_applicable.size] = tag %}
      {% endif %}

      {% if options.product_options_to_consider__array_required contains product.options[2].name %}
        {% assign tag = product.options[2].name | append: "-" | append: variant.option3 %}
        {% assign product_tags_applicable[product_tags_applicable.size] = tag %}
      {% endif %}
    {% endfor %}

    {% for tag in product_tags %}
      {% assign tag_parts = tag | split: "-" %}
      {% if options.product_options_to_consider__array_required contains tag_parts[0] %}
        {% unless product_tags_applicable contains tag %}
          {% assign product_tags_to_remove[product_tags_to_remove.size] = tag %}
        {% endunless %}
      {% endif %}
    {% endfor %}

    {% for tag in product_tags_applicable %}
      {% unless product_tags contains tag %}
        {% assign product_tags_to_add[product_tags_to_add.size] = tag %}
      {% endunless %}
    {% endfor %}

    {% if product_tags_to_add != blank or product_tags_to_remove != blank %}
      {% action "shopify" %}
        mutation {
          {% if product_tags_to_add != blank %}
            tagsAdd(
              id: {{ product.admin_graphql_api_id | json }}
              tags: {{ product_tags_to_add | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          {% endif %}
          {% if product_tags_to_remove != blank %}
            tagsRemove(
              id: {{ product.admin_graphql_api_id | json }}
              tags: {{ product_tags_to_remove | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          {% endif %}
        }
      {% endaction %}
    {% endif %}
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Product options to consider
["Color", "Size"]
Run for products when they are created or updated
true