Mechanic Get Mechanic
Task library

Auto-tag orders when another tag is added

Does exactly as it says. :)

Try this task

When it runs

Occurs whenever an order is updated

What you choose

  • Tags to watch for and tags to add
  • Remove tag to add when the corresponding tag to watch for is removed

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.

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

shopify/orders/updated
How task subscriptions work

Option types and flags

Task 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 %}
Mechanic Liquid reference