Auto-tag products by their options with Mechanic.

Mechanic is an automation and 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 product is created, when a product is updated, and when a user triggers the task. Configuration includes product options to consider.

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 product is created (shopify/products/create)
when a product is updated (shopify/products/update)
when a user triggers the task (mechanic/user/trigger)
Options
product options to consider (array, required)
Script
{% if event.preview %}
  {% action "shopify" %}
    [
      "update",
      ["product", 12345],
      {
        "tags": {{ options.product_options_to_consider__array_required.first | append: "-Blue" | json }}
      }
    ]
  {% endaction %}
{% elsif event.topic contains "shopify/products/" %}
  {% assign product_tags = product.tags %}

  {% for product_option in options.product_options_to_consider__array_required %}
    {% assign product_tags_array = product_tags | split: ", " %}
    {% for product_tag in product_tags %}
      {% assign product_tag_parts = product_tag | split: "-" %}
      {% if product_tag_parts[0] == product_option %}
        {% assign product_tags = product_tags | remove_tag: product_tag %}
      {% endif %}
    {% endfor %}
  {% endfor %}

  {% 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 = product_tags | add_tag: 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 = product_tags | add_tag: 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 = product_tags | add_tag: tag %}
    {% endif %}
  {% endfor %}

  {% if product_tags != product.tags %}
    {% action "shopify" %}
      [
        "update",
        ["product", {{ product.id | json }}],
        {
          "tags": {{ product_tags | json }}
        }
      ]
    {% endaction %}
  {% endif %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% for product in shop.products %}
    {% assign product_tags = product.tags %}

    {% for product_option in options.product_options_to_consider__array_required %}
      {% assign product_tags_array = product_tags | split: ", " %}
      {% for product_tag in product_tags %}
        {% assign product_tag_parts = product_tag | split: "-" %}
        {% if product_tag_parts[0] == product_option %}
          {% assign product_tags = product_tags | remove_tag: product_tag %}
        {% endif %}
      {% endfor %}
    {% endfor %}

    {% 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 = product_tags | add_tag: 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 = product_tags | add_tag: 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 = product_tags | add_tag: tag %}
      {% endif %}
    {% endfor %}

    {% if product_tags != product.tags %}
      {% action "shopify" %}
        [
          "update",
          ["product", {{ product.id | json }}],
          {
            "tags": {{ product_tags | json }}
          }
        ]
      {% 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"]