Mechanic Get Mechanic
Task library

Auto-tag products in a manual collection

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.

Try this task

When it runs

  • Occurs when a user manually triggers the task
  • Occurs whenever a collection is updated, including when a product is manually added or removed from the collection or when the collection rules change

What you choose

  • Collection tag
  • Collection ID

Documentation

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.

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.

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

mechanic/user/trigger
shopify/collections/update
How task subscriptions work

Option types and flags

Task code

{% assign collection_tag = options.collection_tag__required %}
{% assign collection_id = options.collection_id__number_required %}

{% assign collection_tag_lower = collection_tag | downcase %}

{% if event.topic == "shopify/collections/update" %}
  {% comment %}
    -- on collection update, exit if this is not the configured collection
  {% endcomment %}

  {% unless collection.id == collection_id or event.preview %}
    {% break %}
  {% endunless %}
{% endif %}

{% comment %}
  -- get all of the product IDs currently in the collection
{% endcomment %}

{% assign cursor = nil %}
{% assign collection_product_ids = array %}

{% for n in (1..100) %}
  {% capture query %}
    query {
      collection(id: {{ collection_id | prepend: "gid://shopify/Collection/" | json }}) {
        products(
          first: 250
          after: {{ cursor | json }}
        ) {
          pageInfo {
            hasNextPage
            endCursor
          }
          nodes {
            id
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "collection": {
            "products": {
              "nodes": [
                {
                  "id": "gid://shopify/Product/1234567890"
                }
              ]
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = result_json | parse_json %}
  {% endif %}

  {% assign collection_product_ids
    = result.data.collection.products.nodes
    | map: "id"
    | concat: collection_product_ids
  %}

  {% if result.data.collection.products.pageInfo.hasNextPage %}
    {% assign cursor = result.data.collection.products.pageInfo.endCursor %}
  {% else %}
    {% break %}
  {% endif %}
{% endfor %}

{% comment %}
  -- get IDs for all products with collection tag
{% endcomment %}

{% assign cursor = nil %}
{% assign tagged_product_ids = array %}

{% for n in (0..100) %}
  {% capture query %}
    query {
      products(
        first: 250
        after: {{ cursor | json }}
        query: {{ collection_tag | json | prepend: "tag:" | json }}
      ) {
        pageInfo {
          hasNextPage
          endCursor
        }
        nodes {
          id
          tags
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "products": {
            "nodes": [
              {
                "id": "gid://shopify/Product/2345678901",
                "tags": {{ collection_tag | json }}
              }
            ]
          }
        }
      }
    {% endcapture %}

    {% assign result = result_json | parse_json %}
  {% endif %}

  {% comment %}
    -- double check that the collection tag exists on each product to avoid occasional query filter quirkiness
  {% endcomment %}

  {% for product in result.data.products.nodes %}
    {% assign product_tags_lower = product.tags | json | downcase | parse_json %}

    {% if product_tags_lower contains collection_tag_lower %}
      {% assign tagged_product_ids = tagged_product_ids | push: product.id %}
    {% endif %}
  {% endfor %}

  {% if result.data.products.pageInfo.hasNextPage %}
    {% assign cursor = result.data.products.pageInfo.endCursor %}
  {% else %}
    {% break %}
  {% endif %}
{% endfor %}

{% comment %}
  -- add and remove the collection tag from products
{% endcomment %}

{% for collection_product_id in collection_product_ids %}
  {% unless tagged_product_ids contains collection_product_id %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: {{ collection_product_id | json }}
          tags: {{ collection_tag | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endunless %}
{% endfor %}

{% for tagged_product_id in tagged_product_ids %}
  {% unless collection_product_ids contains tagged_product_id %}
    {% action "shopify" %}
      mutation {
        tagsRemove(
          id: {{ tagged_product_id | json }}
          tags: {{ collection_tag | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endunless %}
{% endfor %}
Mechanic Liquid reference

Default option values

Collection tag
sale