Auto-tag orders using the customer's tags with Mechanic.

Mechanic is the one-tool-does-it-all automation app for Shopify. :)

Auto-tag orders using the customer's tags

by Isaac Bowen (team@usemechanic.com)

Use this task to apply the same categorizations you use for your customers. Useful for pre-approving customers, and then quickly sorting your orders based on that pre-approval. Optionally, configure this task to only copy certain tags from customers to their orders.

Runs when an order is created. Configuration includes only copy these tags.

15-day free trial – unlimited tasks

Documentation

{% if event.preview %}
{% assign customer_tags = options.only_copy_these_tags | default: "wholesale, approved" | split: ", " %}
{% else %}
{% assign customer_tags = order.customer.tags | split: ", " %}
{% capture log_message %}The customer has these tags: {{ customer_tags | json }}{% endcapture %}
{"log": {{ log_message | json }}}
{% endif %}

{% if options.only_copy_these_tags == blank %}
{% assign tags_to_copy = customer_tags %}
{% else %}
{% assign tags_to_copy = "" %}

{% assign whitelisted_tags_with_possible_whitespace = options.only_copy_these_tags | split: "," %}
{% capture whitelisted_tags_string %}{% for tag in whitelisted_tags_with_possible_whitespace %}{{ tag | strip }}{% unless forloop.last %},{% endunless %}{% endfor %}{% endcapture %}
{% assign whitelisted_tags = whitelisted_tags_string | split: "," %}

{% capture log_message %}Whitelist is set to these tags: {{ whitelisted_tags | json }}{% endcapture %}
{"log": {{ log_message | json }}}

{% for whitelisted_tag in whitelisted_tags %}
{% if customer_tags contains whitelisted_tag %}
{% assign tags_to_copy = tags_to_copy | add_tag: whitelisted_tag %}
{% endif %}
{% endfor %}
{% endif %}

{% assign tags_to_save = order.tags | add_tags: tags_to_copy %}
{% if tags_to_save == order.tags and event.preview != true %}
{"log": "The order already has all applicable tags from this customer, so there's nothing to update."}
{% else %}
{
"action": {
"type": "shopify",
"options": [
"update",
[
"order",
{{ order.id | json }}
],
{
"tags": {{ tags_to_save | json }}
}
]
}
}
{% endif %}

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
only copy these tags
Script
{% if event.preview %}
  {% assign customer_tags = options.only_copy_these_tags | default: "wholesale, approved" | split: ", " %}
{% else %}
  {% assign customer_tags = order.customer.tags | split: ", " %}
  {% capture log_message %}The customer has these tags: {{ customer_tags | json }}{% endcapture %}
  {"log": {{ log_message | json }}}
{% endif %}

{% if options.only_copy_these_tags == blank %}
  {% assign tags_to_copy = customer_tags %}
{% else %}
  {% assign tags_to_copy = "" %}

  {% assign whitelisted_tags_with_possible_whitespace = options.only_copy_these_tags | split: "," %}
  {% capture whitelisted_tags_string %}{% for tag in whitelisted_tags_with_possible_whitespace %}{{ tag | strip }}{% unless forloop.last %},{% endunless %}{% endfor %}{% endcapture %}
  {% assign whitelisted_tags = whitelisted_tags_string | split: "," %}

  {% capture log_message %}Whitelist is set to these tags: {{ whitelisted_tags | json }}{% endcapture %}
  {"log": {{ log_message | json }}}

  {% for whitelisted_tag in whitelisted_tags %}
    {% if customer_tags contains whitelisted_tag %}
      {% assign tags_to_copy = tags_to_copy | add_tag: whitelisted_tag %}
    {% endif %}
  {% endfor %}
{% endif %}

{% assign tags_to_save = order.tags | add_tags: tags_to_copy %}
{% if tags_to_save == order.tags and event.preview != true %}
  {"log": "The order already has all applicable tags from this customer, so there's nothing to update."}
{% else %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "update",
        [
          "order",
          {{ order.id | json }}
        ],
        {
          "tags": {{ tags_to_save | json }}
        }
      ]
    }
  }
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.