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, or whenever a product is ordered, or whenever a variant is 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), 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, 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
{% 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 }}
      {% endlog %}
    {% 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 %}
      {% error %}
        {{ collection_id_to_join | append: " did not resolve to an existing collection." | json }}
      {% enderror %}
    {% 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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more