Tag orders in bulk by order name, with Mechanic.

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

Tag orders in bulk by order name

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 Occurs when a user manually triggers the task with text.

15-day free trial – unlimited tasks

Documentation

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.

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, 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
mechanic/user/text
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Code
{% 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_one = query_batch_count | minus: 1 %}

  {% for n in (0..query_batch_count_minus_one) %}
    {% 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 %}

    {% assign cursor = nil %}
    {% for m in (0..1000) %}
      {% capture query %}
        query {
          orders(
            first: {{ query_batch_size | json }} 
            after: {{ cursor | json }}
            query: {{ query_friendly_order_names | join: " OR " | json }}
          ) {
            pageInfo {
              hasNextPage
            }
            edges {
              cursor
              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 %}

      {% if result.data.orders.pageInfo.hasNextPage %}
        {% assign cursor = result.data.orders.edges.last.cursor %}
      {% else %}
        {% break %}
      {% 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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more