Auto-tag orders when another tag is added, with Mechanic.

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

Auto-tag orders when another tag is added

Does exactly as it says. :)

Runs Occurs whenever an order is updated. Configuration includes tags to watch for and tags to add and remove tag to add when the corresponding tag to watch for is removed.

15-day free trial – unlimited tasks

Documentation

Does exactly as it says. :)

Configure this task with order tags to watch for on the left, and associated tags to add on the right. (Feel free to use a comma-delimited list on the right side, too.) This task will run whenever an order is created or updated, tagging as configured.

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/orders/updated
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
tags to watch for and tags to add (keyval, required), remove tag to add when the corresponding tag to watch for is removed (boolean)
Code
{% if event.preview %}
  {% assign order = hash %}
  {% assign order["admin_graphql_api_id"] = "gid://shopify/Order/1234567890" %}
  {% assign order["tags"] = options.tags_to_watch_for_and_tags_to_add__keyval_required.first.first %}
{% endif %}

{% assign existing_tags = order.tags | split: ", " %}
{% assign deserved_tags = array %}

{% assign tags_to_add = array %}
{% assign tags_to_remove = array %}

{% for keyval in options.tags_to_watch_for_and_tags_to_add__keyval_required %}
  {% assign tag_to_watch_for = keyval[0] %}
  {% unless existing_tags contains tag_to_watch_for %}
    {% continue %}
  {% endunless %}

  {% assign tags = keyval[1] | replace: ", ", "," | split: "," %}
  {% assign deserved_tags = deserved_tags | concat: tags %}

  {% for tag in tags %}
    {% unless existing_tags contains tag %}
      {% assign tags_to_add[tags_to_add.size] = tag %}
    {% endunless %}
  {% endfor %}
{% endfor %}

{% if options.remove_tag_to_add_when_the_corresponding_tag_to_watch_for_is_removed__boolean %}
  {% for keyval in options.tags_to_watch_for_and_tags_to_add__keyval_required %}
    {% assign tag_to_watch_for = keyval[0] %}
    {% if existing_tags contains tag_to_watch_for %}
      {% continue %}
    {% endif %}

    {% assign tags = keyval[1] | replace: ", ", "," | split: "," %}

    {% for tag in tags %}
      {% unless existing_tags contains tag %}
        {% continue %}
      {% endunless %}

      {% if deserved_tags contains tag %}
        {% continue %}
      {% endif %}

      {% assign tags_to_remove[tags_to_remove.size] = tag %}
    {% endfor %}
  {% endfor %}
{% endif %}

{% if tags_to_add != empty or tags_to_remove != empty %}
  {% action "shopify" %}
    mutation {
      {% if tags_to_add != empty %}
        tagsAdd(
          id: {{ order.admin_graphql_api_id | json }}
          tags: {{ tags_to_add | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      {% endif %}

      {% if tags_to_remove != empty %}
        tagsRemove(
          id: {{ order.admin_graphql_api_id | json }}
          tags: {{ tags_to_remove | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      {% endif %}
    }
  {% endaction %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more