Auto-tag products in a manual collection, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Auto-tag products in a manual collection

by Isaac Bowen (isaac@lightward.com)

If you prefer to categorize your products using manual collections, but you still need your tags to match up, use this task to monitor your manual collection and automatically tag the products it contains.

Runs when a user triggers the task and when a collection is updated. Configuration includes collection tag and collection.

15-day free trial – unlimited tasks

Documentation

Run this task manually to auto-tag products in your configured collection, and to untag any products that are not in this collection. Otherwise, this task will run automatically whenever the configured collection is updated, which includes the addition or removal of products.

This task only supports manual collections, not automated collections. (Learn about the differences here.) Configure this task with your collection ID. Learn how to find this.

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 collection is updated (shopify/collections/update)
Options
collection tag (required), collection (number, required)
Script
{% assign collection_tag = options.collection_tag__required %}

{% if event.preview %}
  {% action "shopify" %}
    mutation {
      tagsAdd(
        id: "gid://shopify/Product/1234567890"
        tags: {{ collection_tag | json }}
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% else %}
  {% assign collection_qualifies = false %}

  {% if event.topic contains "shopify/collections/" and collection.id == options.collection_id__number_required %}
    {% assign collection_qualifies = true %}
  {% elsif event.topic == "mechanic/user/trigger" %}
    {% assign collection_qualifies = true %}
    {% assign collection = shop.collections[options.collection_id__number_required] %}
  {% endif %}

  {% if collection_qualifies %}
    {% assign collection_product_ids = collection.products | map: "id" %}

    {% for product in shop.products %}
      {% assign product_tags = product.tags | split: ", " %}

      {% if collection_product_ids contains product.id %}
        {% unless product_tags contains collection_tag %}
          {% action "shopify" %}
            mutation {
              tagsAdd(
                id: {{ product.admin_graphql_api_id | json }}
                tags: {{ collection_tag | json }}
              ) {
                userErrors {
                  field
                  message
                }
              }
            }
          {% endaction %}
        {% endunless %}
      {% elsif product_tags contains collection_tag %}
        {% action "shopify" %}
          mutation {
            tagsRemove(
              id: {{ product.admin_graphql_api_id | json }}
              tags: {{ collection_tag | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          }
        {% endaction %}
      {% endif %}
    {% endfor %}
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Collection tag
sale