Mechanic is a development and ecommerce automation platform for Shopify. :)
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.
Runs Occurs when a user manually triggers the task and Occurs whenever a collection is updated, including whenever products are added or removed from the collection. Configuration includes collection tag and collection id.
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.
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?)
mechanic/user/trigger shopify/collections/update
{% 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 %}
sale