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

Mechanic is an automation development platform for Shopify. :)

Auto-tag orders based on cart attributes

by Isaac Bowen (team@usemechanic.com)

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 tags to add.

15-day free trial – unlimited tasks

Documentation

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 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.

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

  {{ options.cart_attribute_to_monitor__required }}
  {{ options.cart_attribute_values_and_tags_to_add__keyval_required }}
{% endcomment %}

{% if event.preview %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "update",
        [
          "order",
          1234567890
        ],
        {
          "tags": {{ options.cart_attribute_values_and_tags_to_add__keyval_required.first.first | json }}
        }
      ]
    }
  }
{% else %}
  {% assign attribute_name = options.cart_attribute_to_monitor__required %}
  {% assign attribute_value = order.note_attributes[attribute_name] %}
  {% assign tag = options.cart_attribute_values_and_tags_to_add__keyval_required[attribute_value] %}

  {% if tag != blank %}
    {
      "action": {
        "type": "shopify",
        "options": [
          "update",
          [
            "order",
            {{ order.id | json }}
          ],
          {
            "tags": {{ order.tags | add_tag: tag | json }}
          }
        ]
      }
    }
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.