Auto-tag products in a manual collection with Mechanic.

Mechanic is the one-tool-does-it-all automation app 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 collection is created and when a collection is updated. Configuration includes collection tag and collection.

15-day free trial – unlimited tasks

Documentation

This task only supports manual collections, not automated collections. (Learn about the differences here.)

To find your collection ID, open up the Shopify admin, and open the settings for the collection you'd like to monitor. The URL in your browser will look like this:

https://example.myshopify.com/admin/collections/1234567890

The last set of numbers (in this example, 1234567890) is your collection ID.

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 collection is created (shopify/collections/create)
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": {
      "type": "shopify",
      "options": [
        "update",
        ["product", 12345],
        {
          "tags": {{ collection_tag | json }}
        }
      ]
    }
  }
{% elsif collection.id == options.collection_id__number_required %}
  {% assign collection_product_ids = collection.products | map: "id" %}

  {% for product in shop.products %}
    {% assign product_tags_to_save = product.tags %}
    {% if collection_product_ids contains product.id %}
      {% assign product_tags_to_save = product_tags_to_save | add_tag: collection_tag %}
    {% else %}
      {% assign product_tags_to_save = product_tags_to_save | remove_tag: collection_tag %}
    {% endif %}

    {% if product_tags_to_save != product.tags %}
      {
        "action": {
          "type": "shopify",
          "options": [
            "update",
            ["product", {{ product.id | json }}],
            {
              "tags": {{ product_tags_to_save | json }}
            }
          ]
        }
      }
    {% 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
Collection tag
sale