Mechanic Get Mechanic
Task library

Auto-add products to a custom collection when tagged

Just as it says. Useful for collections that can't be configured for automatic products, but still should have some products pulled in automatically.

Try this task

When it runs

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

What you choose

Product tags and collection IDs

Documentation

Just as it says. Useful for collections that can't be configured for automatic products, but still should have some products pulled in automatically.

This task runs in response to products being tagged. Configure the "Product tags and collection IDs" option with product tags on the left (case-insensitive), each paired with a collection ID on the right. The collection must be a custom collection - one that has its products chosen manually. Learn how to find collection IDs here.

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

shopify/products/create
shopify/products/update
How task subscriptions work

Option types and flags

Task code

{% assign product_tags_and_collection_ids = options.product_tags_and_collection_ids__keyval_number_required %}
{% assign collection_ids = product_tags_and_collection_ids | values %}

{% comment %}
  -- get product tags and collection memberships
{% endcomment %}

{% capture query %}
  query {
    product(id: {{ product.admin_graphql_api_id | json }}) {
      id
      tags
      {% for collection_id in collection_ids %}
        in_collection_{{ collection_id }}: inCollection(id: {{ collection_id | prepend: "gid://shopify/Collection/" | json }})
      {% endfor %}
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "product": {
          "id": "gid://shopify/Product/1234567890",
          "tags": {{ product_tags_and_collection_ids | keys | json }}
        }
      }
    }
  {% endcapture %}

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

{% assign product = result.data.product %}
{% assign product_tags_lower = product.tags | json | downcase | parse_json %}

{% comment %}
  -- check for product tag matches to configured values
{% endcomment %}

{% assign collection_ids_to_join = array %}

{% for keyval in product_tags_and_collection_ids %}
  {% assign product_tag_to_match = keyval[0] | downcase %}
  {% assign collection_id = keyval[1] %}
  {% assign collection_gid = collection_id | prepend: "gid://shopify/Collection/" %}
  {% assign collection_alias = "in_collection_" | append: collection_id %}

  {% if product_tags_lower contains product_tag_to_match %}
    {% unless product[collection_alias] %}
      {% assign collection_ids_to_join = collection_ids_to_join | push: collection_gid %}
    {% endunless %}
  {% endif %}
{% endfor %}

{% comment %}
  -- add product to any qualifying collections
{% endcomment %}

{% for collection_id_to_join in collection_ids_to_join %}
  {% action "shopify" %}
    mutation {
      collectionAddProducts(
        id: {{ collection_id_to_join | json }}
        productIds: {{ array | push: product.id | json }}
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% endfor %}
Mechanic Liquid reference