Mechanic Get Mechanic
Task library

Auto-tag cancelled orders

This task watches for order cancellations, and adds or removes tags according to your preference. Run this task manually to update tags for existing cancelled orders.

Try this task

When it runs

  • Occurs whenever an order is updated
  • Occurs when a user manually triggers the task

What you choose

  • Tags to add
  • Tags to remove
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
mechanic/user/trigger
How task subscriptions work

Option types and flags

Task code

{% assign orders = array %}

{% if event.preview %}
  {% assign orders[0] = '{"admin_graphql_api_id": "gid://shopify/Order/1234567890", "tags": ""}' | parse_json %}
{% elsif event.topic contains "shopify/orders" %}
  {% if order.cancelled_at != blank %}
    {% assign orders[0] = order %}
  {% endif %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% assign orders = shop.orders.any.cancelled %}
{% endif %}

{% for order in orders %}
  {% assign order_tags = order.tags | split: ", " %}

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

  {% for tag in options.tags_to_add__array %}
    {% unless order_tags contains tag %}
      {% assign tags_to_add[tags_to_add.size] = tag %}
    {% endunless %}
  {% endfor %}

  {% for tag in options.tags_to_remove__array %}
    {% if order_tags contains tag %}
      {% assign tags_to_remove[tags_to_remove.size] = tag %}
    {% endif %}
  {% endfor %}

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

Default option values

Tags to add
["cancelled"]