Mechanic Get Mechanic
Task library

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.

Try this task

When it runs

Occurs whenever a product is updated, ordered, or variants are added, removed or updated

What you choose

  • Tags to watch for and tags to add
  • Remove tags to add when the corresponding tag to watch for is removed
  • Manual bulk mode

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.

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

{% if options.manual_bulk_mode__boolean %}
  mechanic/user/trigger
{% else %}
  shopify/products/update
{% endif %}
How task subscriptions work

Option types and flags

Task 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 %}
Mechanic Liquid reference

Default option values

Manual bulk mode
false