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

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

Auto-tag orders with their tracking numbers

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 Occurs whenever a fulfillment is created, Occurs whenever a fulfillment is updated, Occurs when a user manually triggers the task, and Occurs when a bulk operation is completed.

15-day free trial – unlimited tasks

Documentation

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.

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, 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/fulfillments/create
shopify/fulfillments/update
mechanic/user/trigger
mechanic/shopify/bulk_operation
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Code
{% 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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more