Auto-tag orders by financial status, with Mechanic.

Mechanic is a development platform for Shopify. :)

Auto-tag orders by financial status

by Isaac Bowen (team@usemechanic.com)

This task watches new and updated orders, and updates their tags according to their financial status.

Runs when an order is created and when an order is updated. Configuration includes financial statuses and tags and remove outdated financial status tags.

15-day free trial – unlimited tasks

Documentation

This task watches new and updated orders, and updates their tags according to their financial status.

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 an order is created (shopify/orders/create)
when an order is updated (shopify/orders/updated)
Options
financial statuses and tags (required, keyval), remove outdated financial status tags (boolean)
Script
{% if event.preview %}
  {% assign order = hash %}
  {% assign order["admin_graphql_api_id"] = "gid://shopify/Order/1234567890" %}
  {% assign order["financial_status"] = options.financial_statuses_and_tags__required_keyval.first.first %}
  {% assign order["tags"] = "" %}
{% endif %}

{% assign order_tags = order.tags | split: ", " %}
{% assign tags_to_remove = array %}
{% assign tag_to_add = nil %}
{% assign valid_financial_statuses = "pending authorized partially_paid paid partially_refunded refunded voided" | split: " " %}

{% for pair in options.financial_statuses_and_tags__required_keyval %}
  {% assign financial_status = pair[0] %}
  {% assign tag_for_financial_status = pair[1] %}

  {% unless valid_financial_statuses contains financial_status %}
    {% capture message -%}
      The financial status {{ financial_status | json }} is not valid. Only these values are supported: {{ valid_financial_statuses | join: ", " }}.
    {%- endcapture %}
    {% error message %}
  {% endunless %}

  {% if order.financial_status == financial_status %}
    {% if order_tags contains tag_for_financial_status %}
      {% log message: "Order already has the right tag", financial_status: financial_status, tag: tag_for_financial_status %}
    {% elsif tag_for_financial_status != blank %}
      {% assign tag_to_add = tag_for_financial_status %}
    {% endif %}
  {% elsif options.remove_outdated_financial_status_tags__boolean and order_tags contains tag_for_financial_status %}
    {% assign tags_to_remove[tags_to_remove.size] = tag_for_financial_status %}
  {% endif %}
{% endfor %}

{% if tag_to_add or tags_to_remove != empty %}
  {% action "shopify" %}
    mutation {
      {% if tag_to_add != blank %}
        tagsAdd(
          id: {{ order.admin_graphql_api_id | json }}
          tags: {{ tag_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 tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Financial statuses and tags
{"pending"=>"payment-pending", "authorized"=>"", "partially_paid"=>"", "paid"=>"", "partially_refunded"=>"", "refunded"=>"", "voided"=>""}
Remove outdated financial status tags
true