Auto-tag orders with customer tags when new customer tags are tagged, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Auto-tag orders with customer tags when new customer tags are tagged

by Jed Storie (team@usemechanic.com)

When a customer is updated, this task will go through each of that customer's past orders and add all of the customer's current tags as order tags (if any new tags are found).

Runs when a customer is updated.

15-day free trial – unlimited tasks

Documentation

Activating this task will automatically keep the order tags up to date with the customer's tags.

When a customer is updated, this task will go through each of that customer's past orders and add all of the customer's current tags as order tags (if any new tags are found).

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 a customer is updated (shopify/customers/update)
Script
{% assign customer_tags = customer.tags | split: ", " %}

{% if event.preview %}
  {% action "shopify" %}
    mutation {
      tagsAdd(
        id: "gid://shopify/Order/123456"
        tags: ["foo"]
      ) { 
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% else %}
  {% for order in customer.orders.any %}
    {% assign order_tags = order.tags | split: ", " %}
    {% assign tags_to_add = array %}

    {% for tag in customer_tags %}
      {% unless order_tags contains tag %}
        {% assign tags_to_add[tags_to_add.size] = tag %}
      {% endunless %}
    {% 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 %}
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.