Auto-tag orders based on cart attributes, with Mechanic.

Mechanic is a development platform for Shopify. :)

Auto-tag orders based on cart attributes

by Isaac Bowen (

Use this task to automatically categorize orders based on the cart attributes configured before checkout. Useful if you allow customers to supply extra information about their order, or if your cart has some hidden coding that adds cart attributes programatically.

Runs when an order is created. Configuration includes cart attribute to monitor and cart attribute values and order tags to add.

15-day free trial – unlimited tasks


Configure the "Cart attribute to monitor" option with the exact name of the cart attribute in use. This value is case-sensitive, and must include any punctuation used in the actual cart attribute.

Configure the "Cart attribute values and order tags to add" option with an attribute value on the left, and a tag on the right, to be applied when the left-hand value is found on an order. Add as many value/tag pairs as you like. :)

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.

when an order is created (shopify/orders/create)
cart attribute to monitor (required), cart attribute values and order tags to add (keyval, required)
{% comment %}
  Preferred order of options:

  {{ options.cart_attribute_to_monitor__required }}
  {{ options.cart_attribute_values_and_order_tags_to_add__keyval_required }}
{% endcomment %}

{% if event.preview %}
  {% capture order_json %}
      "admin_graphql_api_id": "gid://shopify/Order/12345",
      "tags": "",
      "note_attributes": {
        {{ options.cart_attribute_to_monitor__required | json }}: {{ options.cart_attribute_values_and_order_tags_to_add__keyval_required.first.first | json }}
  {% endcapture %}

  {% assign order = order_json | parse_json %}
{% endif %}

{% assign attribute_name = options.cart_attribute_to_monitor__required %}
{% assign attribute_value = order.note_attributes[attribute_name] %}
{% assign tag = options.cart_attribute_values_and_order_tags_to_add__keyval_required[attribute_value] %}
{% assign order_tags = order.tags | split: ", " %}

{% if tag == blank %}
  {% log "Order does not qualify for a tag" %}
{% elsif order_tags contains tag %}
  {% log message: "Order is already tagged", tag: tag %}
{% else %}
  {% action "shopify" %}
    mutation {
        id: {{ order.admin_graphql_api_id | json }}
        tags: {{ tag | json }}
      ) {
        userErrors {
  {% endaction %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.