Auto-tag products when another tag is added, with Mechanic.

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

Auto-tag products when another tag is added

This task runs whenever a product is updated (which includes product creation), and it will add the configured "tags to add" to the product when the corresponding "tags to watch for" are present.

Runs Occurs whenever a product is updated, or whenever a product is ordered, or whenever a variant is added, removed, or updated. Configuration includes tags to watch for and tags to add, remove tags to add when the corresponding tag to watch for is removed, and manual bulk mode.

15-day free trial – unlimited tasks

Documentation

This task runs whenever a product is updated (which includes product creation), and it will add the configured "tags to add" to the product when the corresponding "tags to watch for" are present.

Configure this task with product tags to watch for on the left, and associated tags to add on the right. (Feel free to use a comma-delimited list on the right side, too.) Optionally, you may choose to remove the "tags to add" whenever its paired "tag to watch for" is absent.

This task also supports a manual bulk mode for scanning ALL products in the shop. Select the manual bulk mode option (and save the task) to switch to manual mode and display the "Run task" button.

Note: Make sure to switch the manual bulk mode off when the full scan is complete if you want the task to resume responding to product update events.

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
{% if options.manual_bulk_mode__boolean %}
  mechanic/user/trigger
{% else %}
  shopify/products/update
{% endif %}
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
tags to watch for and tags to add (keyval, required), remove tags to add when the corresponding tag to watch for is removed (boolean), manual bulk mode (boolean)
Code
{% assign tags_to_watch_for_and_tags_to_add = options.tags_to_watch_for_and_tags_to_add__keyval_required %}
{% assign remove_tags_to_add = options.remove_tags_to_add_when_the_corresponding_tag_to_watch_for_is_removed__boolean %}

{% assign products = array %}

{% if event.topic == "shopify/products/update" %}
  {% assign products[0] = hash %}
  {% assign products[0]["id"] = product.admin_graphql_api_id %}
  {% assign products[0]["tags"] = product.tags | split: ", " %}

{% elsif event.topic == "mechanic/user/trigger" %}
  {% assign cursor = nil %}
      
  {% for n in (1..100) %}
    {% capture query %}
      query {
        products(
          first: 250
          after: {{ cursor | json }}
        ) {
          pageInfo {
            hasNextPage
            endCursor
          }
          nodes {
            id
            tags
          }
        }
      }
    {% endcapture %}
  
    {% assign result = query | shopify %}
  
    {% assign products
      = result.data.products.nodes
      | default: array
      | concat: products
    %}
  
    {% if result.data.products.pageInfo.hasNextPage %}
      {% assign cursor = result.data.products.pageInfo.endCursor %}
    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}
{% endif %}

{% if event.preview %}
  {% assign products[0] = hash %}
  {% assign products[0]["id"] = "gid://shopify/Product/1234567890" %}
  {% assign products[0]["tags"] = tags_to_watch_for_and_tags_to_add.first.first %}
{% endif %}

{% for product in products %}
  {% assign deserved_tags = array %}
  {% assign tags_to_add = array %}
  {% assign tags_to_remove = array %}

  {% for keyval in tags_to_watch_for_and_tags_to_add %}
    {% assign tag_to_watch_for = keyval[0] %}

    {% if product.tags contains tag_to_watch_for %}
      {% assign tags = keyval[1] | replace: ", ", "," | split: "," %}
      {% assign deserved_tags = deserved_tags | concat: tags %}

      {% for tag in tags %}
        {% unless product.tags contains tag %}
          {% assign tags_to_add[tags_to_add.size] = tag %}
        {% endunless %}
      {% endfor %}
    {% endif %}
  {% endfor %}

  {% if remove_tags_to_add %}
    {% for keyval in tags_to_watch_for_and_tags_to_add %}
      {% assign tag_to_watch_for = keyval[0] %}

      {% if product.tags contains tag_to_watch_for %}
        {% continue %}
      {% endif %}

      {% assign tags = keyval[1] | replace: ", ", "," | split: "," %}

      {% for tag in tags %}
        {% unless product.tags contains tag %}
          {% continue %}
        {% endunless %}

        {% if deserved_tags contains tag %}
          {% continue %}
        {% endif %}

        {% assign tags_to_remove[tags_to_remove.size] = tag %}
      {% endfor %}
    {% endfor %}
  {% endif %}

  {% if tags_to_add != empty or tags_to_remove != empty %}
    {% action "shopify" %}
      mutation {
        {% if tags_to_add != empty %}
          tagsAdd(
            id: {{ product.id | json }}
            tags: {{ tags_to_add | json }}
          ) {
            node {
              ... on Product {
                id
                title
                tags_after_add: tags
              }
            }
            userErrors {
              field
              message
            }
          }
        {% endif %}
        {% if tags_to_remove != empty %}
          tagsRemove(
            id: {{ product.id | json }}
            tags: {{ tags_to_remove | json }}
          ) {
            node {
              ... on Product {
                id
                title
                tags_after_remove: tags
              }
            }
            userErrors {
              field
              message
            }
          }
        {% endif %}
      }
    {% endaction %}
  {% endif %}
{% endfor %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more