Auto-tag orders with their tracking numbers, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Auto-tag orders with their tracking numbers

by Isaac Bowen (team@usemechanic.com)

Tracking numbers aren't generally searchable within Shopify. Use this task to fix that, by copying all fulfillment tracking numbers over to the order itself, as (searchable!) order tags.

Runs when a fulfillment is created, when a fulfillment is updated, when a user triggers the task, and when a bulk operation finishes.

15-day free trial – unlimited tasks

Documentation

This task runs when fulfillments are created or updated, adding the fulfillment's tracking number(s) to the order's tags. Run this task manually to scan and tag all of your store's existing orders.

Please note: any special characters in the tracking number may be ignored by Shopify.

YouTube: Watch the development video!

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 a fulfillment is created (shopify/fulfillments/create)
when a fulfillment is updated (shopify/fulfillments/update)
when a user triggers the task (mechanic/user/trigger)
when a bulk operation finishes (mechanic/shopify/bulk_operation)
Script
{% if event.topic contains "shopify/fulfillments/" %}
  {% if event.preview %}
    {% assign fulfillment = hash %}
    {% assign fulfillment["tracking_numbers"] = "ABC123" | split: ", " %}
    {% assign fulfillment["order"] = hash %}
    {% assign fulfillment["order"]["admin_graphql_api_id"] = "gid://shopify/Order/1234567890" %}
    {% assign fulfillment["order"]["tags"] = "" %}
  {% endif %}

  {% assign order_tags = fulfillment.order.tags | split: ", " %}
  {% assign tags_to_add = array %}

  {% for tracking_number in fulfillment.tracking_numbers %}
    {% if tracking_number != blank %}
      {% unless order_tags contains tracking_number %}
        {% assign tags_to_add[tags_to_add.size] = tracking_number %}
      {% endunless %}
    {% endif %}
  {% endfor %}

  {% if tags_to_add != empty %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: {{ fulfillment.order.admin_graphql_api_id | json }}
          tags: {{ tags_to_add | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% capture bulk_operation_query %}
    query {
      orders {
        edges {
          node {
            id
            tags
            fulfillments {
              trackingInfo {
                number
              }
            }
          }
        }
      }
    }
  {% 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_json %}
      [
        {
          "id": "gid://shopify/Order/1234567890",
          "tags": [],
          "fulfillments": [
            {
              "trackingInfo": [
                {
                  "number": "ABC123"
                }
              ]
            }
          ]
        }
      ]
    {% endcapture %}

    {% assign bulkOperation = hash %}
    {% assign bulkOperation["objects"] = objects_json | parse_json %}
  {% endif %}

  {% for order in bulkOperation.objects %}
    {% assign tags_to_add = array %}

    {% for fulfillment in order.fulfillments %}
      {% for trackingInfoObject in fulfillment.trackingInfo %}
        {% if trackingInfoObject.number != blank %}
          {% unless order.tags contains trackingInfoObject.number %}
            {% assign tags_to_add[tags_to_add.size] = trackingInfoObject.number %}
          {% endunless %}
        {% endif %}
      {% endfor %}
    {% endfor %}

    {% if tags_to_add != empty %}
      {% action "shopify" %}
        mutation {
          tagsAdd(
            id: {{ order.id | json }}
            tags: {{ tags_to_add | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.