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_qualifies = false %} {% if event.topic contains "shopify/collections/" and collection.id == options.collection_id__number_required %} {% assign collection_qualifies = true %} {% elsif event.topic == "mechanic/user/trigger" %} {% assign collection_qualifies = true %} {% assign collection = shop.collections[options.collection_id__number_required] %} {% endif %} {% if collection_qualifies or event.preview %} {% assign collection_product_ids = collection.products | map: "admin_graphql_api_id" %} {% 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 } edges { cursor node { id } } } } {% endcapture %} {% assign result = query | shopify %} {% if event.preview %} {% assign collection_product_ids = array %} {% assign collection_product_ids[0] = "gid://shopify/Product/1234567890" %} {% assign collection_product_ids[1] = "gid://shopify/Product/2345678901" %} {% capture result_json %} { "data": { "products": { "edges": [ { "node": { "id": "gid://shopify/Product/1234567890" } }, { "node": { "id": "gid://shopify/Product/9876543210" } } ] } } } {% endcapture %} {% assign result = result_json | parse_json %} {% endif %} {% assign products_result = result.data.products.edges | map: "node" | map: "id" %} {% assign tagged_product_ids = tagged_product_ids | concat: products_result %} {% if result.data.products.pageInfo.hasNextPage %} {% assign cursor = result.data.products.edges.last.cursor %} {% else %} {% break %} {% endif %} {% endfor %} {% 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 %} {% endif %}
sale