Auto-tag orders with a cart attribute, with Mechanic.

Mechanic is a development platform for Shopify. :)

Auto-tag orders with a cart attribute

by Isaac Bowen (team@usemechanic.com)

This task checks for incoming orders, watching for orders that have a specific cart attribute filled in. When present, the task then copies that attribute over to the order's tags.

Runs when an order is created. Configuration includes cart attribute to monitor.

15-day free trial – unlimited tasks

Documentation

This task checks for incoming orders, watching for orders that have a specific cart attribute filled in. When present, the task then copies that attribute over to the order's tags.

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)
Script
{% if event.preview %}
  {% capture order_json %}
    {
      "admin_graphql_api_id": "gid://shopify/Order/12345",
      "tags": "",
      "note_attributes": {
        {{ options.cart_attribute_to_monitor__required | json }}: "Hello!"
      }
    }
  {% endcapture %}

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

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

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