Auto-add products to a custom collection when tagged, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

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.

Runs Occurs whenever a product is created and Occurs whenever a product is updated, ordered, or variants are added, removed or updated. Configuration includes product tags and collection ids.

15-day free trial – unlimited tasks

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.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, 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.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
shopify/products/create
shopify/products/update
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
product tags and collection ids (keyval, number, required)
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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more