Tag orders in bulk by order number with Mechanic.

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

Tag orders in bulk by order number

by Isaac Bowen (team@usemechanic.com)

Use this task to rapidly update your order list, by entering a set of 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 number + tag pairs, like so:

12345:approved
67890:approved
11111:denied

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

Developer details

Events
when some text is submitted (mechanic/user/text)
Options
Script
{% capture newline %}
{% endcapture %}

{% if event.preview %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "update",
        [
          "order",
          12345
        ],
        {
          "tags": "your-tag"
        }
      ]
    }
  }
{% else %}
  {% assign order_number_and_tag_pairs = event.data | strip | split: newline %}
  {% assign taggings = 0 %}

  {% for order in shop.orders.any %}
    {% capture order_number_string %}{{ order.order_number }}{% endcapture -%}
    {% assign existing_order_tags = order.tags | split: ", " %}

    {% for order_number_and_tag_pair in order_number_and_tag_pairs %}
      {% assign order_number_and_tag_pair_elements = order_number_and_tag_pair | split: ":" %}
      {% assign order_number = order_number_and_tag_pair_elements[0] %}
      {% assign order_tag_to_add = order_number_and_tag_pair_elements[1] %}

      {% if order_number_string == order_number %}
        {% assign taggings = taggings | plus: 1 %}

        {% unless existing_order_tags contains order_tag_to_add %}
          {% capture order_tags_to_save %}{% if order.tags != blank %}{{ order.tags }}, {% endif %}{{ order_tag_to_add }}{% endcapture -%}

          {
            "action": {
              "type": "shopify",
              "options": [
                "update",
                [
                  "order",
                  {{ order.id | json }}
                ],
                {
                  "tags": {{ order_tags_to_save | json }}
                }
              ]
            }
          }
        {% endunless %}
      {% endif -%}
    {% endfor -%}

    {% if order_number_and_tag_pairs.size == taggings -%}
      {% break -%}
    {% endif -%}
  {% endfor -%}

{% endif %}
Yep, that’s just Liquid! If this is interesting to you, check out our documentation! Mechanic is hugely developer-friendly. :)