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

Mechanic is a development and ecommerce automation platform for Shopify. :)

Auto-tag orders based on cart attributes

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 Occurs whenever 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

Documentation

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.

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, 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?)

Open source
View on GitHub to contribute to this task
Subscriptions
shopify/orders/create
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
cart attribute to monitor (required), cart attribute values and order tags to add (keyval, required)
Code
{% 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 {
      tagsAdd(
        id: {{ order.admin_graphql_api_id | json }}
        tags: {{ tag | json }}
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more