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

Mechanic is an automation and development platform for Shopify. :)

Auto-add products to a custom collection when tagged

by Isaac Bowen (team@usemechanic.com)

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 when a product is created and when a product is updated. Configuration includes product tags and collection ids.

15-day free trial – unlimited tasks

Documentation

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), and collection IDs on the right. The collection must be a custom collection - one that has its products chosen manually. (Learn how to find the 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 product is created (shopify/products/create)
when a product is updated (shopify/products/update)
Options
product tags and collection ids (keyval, number, required)
Script
{% if event.preview %}
  {% capture product_json %}
    {
      "admin_graphql_api_id": "gid://shopify/Product/1234567890",
      "collects": [],
      "tags": {{ options.product_tags_and_collection_ids__keyval_number_required.first[0] | json }}
    }
  {% endcapture %}

  {% assign product = product_json | parse_json %}
{% endif %}

{% assign product_tags_to_match = product.tags | downcase | split: ", " %}

{% assign collection_ids = product.collects | map: "collection_id" %}

{% assign collection_ids_to_join = array %}

{% for keyval in options.product_tags_and_collection_ids__keyval_number_required %}
  {% assign product_tag_to_match = keyval[0] | downcase %}
  {% assign collection_id_to_join = keyval[1] | round %}

  {% if product_tags_to_match contains product_tag_to_match %}
    {% if collection_ids contains collection_id_to_join %}
      {"log": {{ "This product is already in collection " | append: collection_id_to_join | append: "; skipping" | json }}}
    {% else %}
      {% assign collection_ids_to_join[collection_ids_to_join.size] = collection_id_to_join %}
    {% endif %}
  {% endif %}
{% endfor %}

{% if collection_ids_to_join != empty %}
  {% for collection_id_to_join in collection_ids_to_join %}
    {% if event.preview %}
      {% assign collection_admin_graphql_api_id = "gid://shopify/Collection/1234567890" %}
    {% else %}
      {% assign collection_admin_graphql_api_id = shop.collections[collection_id_to_join].admin_graphql_api_id %}
    {% endif %}

    {% if collection_admin_graphql_api_id == nil %}
      {% action "echo" %}
        {"error": {{ collection_id_to_join | append: " did not resolve to an existing collection." | json }}}
      {% endaction %}
    {% else %}
      {% action "shopify" %}
        mutation {
          collectionAddProducts(
            id: {{ collection_admin_graphql_api_id | json }}
            productIds: [{{ product.admin_graphql_api_id | json }}]
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.