Tag orders in bulk by order name with Mechanic.

Mechanic is the one-tool-does-it-all automation app for Shopify. :)

Tag orders in bulk by order name

by Isaac Bowen (team@usemechanic.com)

Use this task to rapidly update your order list, by entering a set of order names (e.g. "#12345", often called order numbers) and the tags to apply to those orders.

Runs when some text is submitted.

15-day free trial – unlimited tasks

Documentation

Enter a list of order name + tag pairs, like so:

#12345:approved
#67890:approved
#11111:denied

Please note: the order name must be an exact match, including any prefixes and suffixes.

Mechanic will look up each order by name, and add the tag you specify. (If the tag is already present, Mechanic won't update the order.)

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 some text is submitted (mechanic/user/text)
Script
{% if event.preview %}
  {% action "shopify" %}
    mutation {
      tagsAdd(
        id: "gid://shopify/Order/1234567890"
        tags: "sometag"
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% else %}
  {% assign order_name_and_tag_lines = event.data | strip | split: newline %}
  {% assign order_names_tags = hash %}
  {% assign order_names_ids = hash %}

  {% assign unresolved_order_names = array %}
  {% assign already_tagged_order_names = array %}

  {% assign query_batch_size = 100 %}
  {% assign query_batch_count = order_name_and_tag_lines.size | times: 1.0 | divided_by: query_batch_size | ceil %}
  {% assign query_batch_count_minus_noe = query_batch_count | minus: 1 %}

  {% for n in (0..query_batch_count_minus_noe) %}
    {% assign query_friendly_order_names = array %}

    {% assign offset = n | times: query_batch_size %}
    {% for line in order_name_and_tag_lines limit:query_batch_size offset:offset %}
      {% assign keyval = line | split: ":" %}
      {% assign order_names_tags[keyval.first] = keyval.last %}
      {% assign query_friendly_order_names[query_friendly_order_names.size] = line | split: ":" | first | json %}
    {% endfor %}

    {% capture query %}
      query {
        orders(
          first: {{ query_batch_size | json }} 
          query: {{ query_friendly_order_names | join: " OR " | json }}
        ) {
          edges {
            node {
              id
              name
              tags
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% for order_edge in result.data.orders.edges %}
      {% assign order_node = order_edge.node %}
      {% assign order_names_ids[order_node.name] = order_node.id %}

      {% if order_node.tags contains order_names_tags[order_node.name] %}
        {% assign already_tagged_order_names[already_tagged_order_names.size] = order_node.name %}
      {% endif %}
    {% endfor %}
  {% endfor %}


  {% capture mutations %}
    {% for keyval in order_names_tags %}
      {% assign order_name = keyval[0] %}
      {% assign tag_to_apply = keyval[1] %}
      {% assign order_id = order_names_ids[order_name] %}

      {% if order_id == blank %}
        {% assign unresolved_order_names[unresolved_order_names.size] = order_name %}
      {% elsif already_tagged_order_names contains order_name %}
        {% comment %}do nothing; no need to re-tag{% endcomment %}
      {% else %}
        {% action "shopify" %}
          mutation {
            tagsAdd(
              id: {{ order_id | json }}
              tags: {{ tag_to_apply | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          }
        {% endaction %}
      {% endif %}
    {% endfor %}
  {% endcapture %}

  {% if unresolved_order_names != blank %}
    {"log": ["Failed to locate and tag {{ unresolved_order_names.size }} order(s):", {{ unresolved_order_names | json }}]}
  {% endif %}

  {% if already_tagged_order_names != blank %}
    {"log": ["{{ already_tagged_order_names.size }} order(s) were already tagged:", {{ already_tagged_order_names | json }}]}
  {% endif %}

  {{ mutations }}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.