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

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

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

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

Runs Occurs whenever 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, 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/customers/update
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Code
{% 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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more