Auto-tag orders based on customer account tags, with Mechanic.

Mechanic is a development platform for Shopify. :)

Auto-tag orders based on customer account tags

by Ken Parelius (team@usemechanic.com)

Use this task to apply certain tags to newly-created orders, based on what tags are on the associated customer. Configure this task with one-to-one mappings of customer tags to look for, connected to order tags to add.

Runs when an order is created. Configuration includes customer tags to look for and order tags to add.

15-day free trial – unlimited tasks

Documentation

Use this task to apply certain tags to newly-created orders, based on what tags are on the associated customer. Configure this task with one-to-one mappings of customer tags to look for, connected to order tags to add.

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
customer tags to look for and order tags to add (keyval)
Script
{% if event.preview %}
  {% assign order = hash %}
  {% assign order["admin_graphql_api_id"] = "gid://shopify/Order/123456780" %}
  {% assign order["customer"] = hash %}
  {% assign order["customer"]["tags"] = options.customer_tags_to_look_for_and_order_tags_to_add__keyval.first.first %}
{% endif %}

{% assign customer_tags = order.customer.tags | split: ", " %}
{% assign tags_to_add = array %}
{% assign tag_pairs = options.customer_tags_to_look_for_and_order_tags_to_add__keyval %}

{% for pair in tag_pairs %}
  {% if customer_tags contains pair.first %}
    {% assign tags_to_add = tags_to_add | add_tag: pair.last %}
  {% endif %}
{% endfor %}

{% if tags_to_add != empty %}
 {% action "shopify" %}
  mutation {
    tagsAdd(
      id: {{ order.admin_graphql_api_id | json }}
      tags: {{ tags_to_add | 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.
Defaults
Customer tags to look for and order tags to add
{"wholesale-customer"=>"wholesale-order"}