Auto-tag orders by product collections, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Auto-tag orders by product collections

by Isaac Bowen (team@usemechanic.com)

Use this task to automatically tag orders based on what collection(s) the customer has ordered from. Useful for broadly classifying orders according to their contents.

Runs when an order is created and when a user triggers the task. Configuration includes collections and tags.

15-day free trial – unlimited tasks

Documentation

Fill in the "Collections and tags" option with the collection on the left, and tags on the right. When specifying the collection, you can use the collection title (e.g. "Summer Swimware"), handle ("e.g. summer-swimware"), or ID (e.g. 27359487103). These are all case-sensitive, so double-check your work!

Run this task manually to scan and tag every order in your store.

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 an order is created (shopify/orders/create)
when a user triggers the task (mechanic/user/trigger)
Options
collections and tags (keyval, required)
Script
{% assign orders = array %}

{% if event.preview %}
  {% action "shopify" %}
    mutation {
      tagsAdd(
        id: "gid://shopify/Order/1234567890"
        tags: {{ options.collections_and_tags__keyval_required.first.last | json }}
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% elsif event.topic contains "shopify/orders/" %}
  {% assign orders[0] = order %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% assign orders = shop.orders.any %}
{% endif %}

{% for order in orders %}
  {% assign existing_tags = order.tags | split: ", " %}
  {% assign tags_to_add = array %}

  {% unless event.preview %}
    {% assign order_collections = nil %}
    {% for line_item in order.line_items %}
      {% if line_item.product_id != blank %}
        {% assign order_collections = order_collections | concat: line_item.product.collections %}
      {% endif %}
    {% endfor %}
    {% assign order_collection_ids = order_collections | map: "id" | join: "," | split: "," %}
    {% assign order_collection_titles = order_collections | map: "title" %}
    {% assign order_collection_handles = order_collections | map: "handle" %}
  {% endunless %}

  {% for pair in options.collections_and_tags__keyval_required %}
    {% assign required_collection = pair[0] %}
    {% assign tags = pair[1] | split: "," %}

    {% if order_collection_ids contains required_collection
          or order_collection_titles contains required_collection
          or order_collection_handles contains required_collection
    %}
      {% for tag in tags %}
        {% assign stripped_tag = tag | strip %}
        {% unless existing_tags contains stripped_tag %}
          {% assign tags_to_add[tags_to_add.size] = stripped_tag %}
        {% endunless %}
      {% endfor %}
    {% endif %}
  {% endfor %}

  {% if tags_to_add != empty %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: {{ order.admin_graphql_api_id | json }}
          tags: {{ tags_to_add | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endfor %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Collections and tags
{"Summer Swimware"=>"summer, swimware", "Cold Weather Gear"=>"winter"}