Auto-tag customers when their order is tagged, with Mechanic.

Mechanic is a development platform for Shopify. :)

Auto-tag customers when their order is tagged

by Isaac Bowen (team@usemechanic.com)

This task watches for order tags, and adds a corresponding tag to the customer whenever a known order tag is found.

Runs when an order is created and when an order is updated. Configuration includes order tags and customer tags.

15-day free trial – unlimited tasks

Documentation

Configure this task with order tags on the left, and customer tags on the right. When an order is created or updated with one or more of the tags on the left, the corresponding customer tags will be applied to the customer. Simple as that. :)

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)
when an order is updated (shopify/orders/updated)
Options
order tags and customer tags (keyval, required)
Script
{% if event.preview %}
  {% capture order_json %}
    {
      "tags": {{ options.order_tags_and_customer_tags__keyval_required.first.first | json }},
      "customer": {
        "admin_graphql_api_id": "gid://shopify/Customer/1234567890",
        "tags": ""
      }
    }
  {% endcapture %}

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

{% assign order_tags = order.tags | split: ", " %}
{% assign customer_tags = order.customer.tags | split: ", " %}

{% assign customer_tags_to_apply = array %}

{% for order_tag in order_tags %}
  {% assign customer_tag_to_apply = options.order_tags_and_customer_tags__keyval_required[order_tag] %}
  {% if customer_tag_to_apply != blank %}
    {% if customer_tags contains customer_tag_to_apply %}
      {% log message: "Customer already has this tag; skipping", tag: customer_tag_to_apply %}
    {% else %}
      {% assign customer_tags_to_apply[customer_tags_to_apply.size] = customer_tag_to_apply %}
    {% endif %}
  {% endif %}
{% endfor %}

{% if customer_tags_to_apply != empty %}
  {% action "shopify" %}
    mutation {
      tagsAdd(
        id: {{ order.customer.admin_graphql_api_id | json }}
        tags: {{ customer_tags_to_apply | 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
Order tags and customer tags
{"order-tag-to-watch-for"=>"corresponding-customer-tag-to-apply"}