Sync an inverse order tag, with Mechanic.

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

Sync an inverse order tag

Shopify's order group filters allow you to filter by the tags the order contains – but not the tags a order does not contain. Use this task to sync an "inverse" tag to all orders who do not have a particular tag, allowing you to filter orders in Shopify by that inverse tag.

Runs Occurs whenever an order is created, Occurs whenever an order is updated, Occurs when a user manually triggers the task, and Occurs when a bulk operation is completed. Configuration includes order tag to watch and order tag to use when missing.

15-day free trial – unlimited tasks

Documentation

Shopify's order group filters allow you to filter by the tags the order contains – but not the tags a order does not contain. Use this task to sync an "inverse" tag to all orders who do not have a particular tag, allowing you to filter orders in Shopify by that inverse tag.

To explain by example, if you've already tagged your wholesale orders with "wholesale", configure the "order tag to watch" option with "wholesale", and the "order tag to use when missing" option with "retail". This task will automatically tag all non-wholesale orders with "retail", and automatically remove that tag from any orders who later receive the tag "wholesale". Please note: if you remove the "retail" tag, this task will simply re-add it. To remove the "retail" tag, you would need to add the "wholesale" tag to that order.

Run this task manually to scan all of the orders in your store. Otherwise, this task will monitor and sync tags for all new and updated orders, as they come through.

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/create
shopify/orders/updated
mechanic/user/trigger
mechanic/shopify/bulk_operation
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
order tag to watch (required) , order tag to use when missing (required)
Code
{% assign orders = array %}

{% if event.topic contains "shopify/orders/" %}
  {% if event.preview %}
    {% assign order = hash %}
    {% assign order["admin_graphql_api_id"] = "gid://shopify/order/1234567890" %}
    {% assign order["tags"] = "" %}
  {% endif %}

  {% assign order_node = hash %}
  {% assign order_node["id"] = order.admin_graphql_api_id %}
  {% assign order_node["tags"] = order.tags | split: ", " %}
  {% assign orders[0] = order_node %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% capture bulk_operation_query %}
    query {
      orders {
        edges {
          node {
            id
            tags
          }
        }
      }
    }
  {% endcapture %}

  {% action "shopify" %}
    mutation {
      bulkOperationRunQuery(
        query: {{ bulk_operation_query | json }}
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
  {% if event.preview %}
    {% capture objects_jsonl %}
      {"id":"gid://shopify/order/1234567890","tags": []}
    {% endcapture %}

    {% assign bulkOperation = hash %}
    {% assign bulkOperation["objects"] = objects_jsonl | parse_jsonl %}
  {% endif %}

  {% assign orders = bulkOperation.objects %}
{% endif %}

{% for order in orders %}
  {% assign add_tag = false %}
  {% assign remove_tag = false %}

  {% if order.tags contains options.order_tag_to_watch__required %}
    {% if order.tags contains options.order_tag_to_use_when_missing__required %}
      {% assign remove_tag = true %}
    {% endif %}
  {% else %}
    {% unless order.tags contains options.order_tag_to_use_when_missing__required %}
      {% assign add_tag = true %}
    {% endunless %}
  {% endif %}

  {% if add_tag or remove_tag %}
    {% action "shopify" %}
      mutation {
        tags{% if add_tag %}Add{% else %}Remove{% endif %}(
          id: {{ order.id | json }}
          tags: {{ options.order_tag_to_use_when_missing__required | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endfor %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Order tag to watch
wholesale
Order tag to use when missing
retail