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

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

Auto-tag orders with a cart attribute

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 Occurs whenever an order is created. Configuration includes cart attribute to monitor.

15-day free trial – unlimited tasks

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)
Code
{% 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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more