Auto-prefix tracking numbers for each new fulfillment, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Auto-prefix tracking numbers for each new fulfillment

by Isaac Bowen (team@usemechanic.com)

This task runs immediately after each new fulfillment is created, and adds a prefix to its tracking number, if a tracking number is present.

Runs when a fulfillment is created. Configuration includes tracking number prefix to add and notify customer.

15-day free trial – unlimited tasks

Documentation

This task runs immediately after each new fulfillment is created, and adds a prefix to its tracking number, if a tracking number is present.

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)
Options
tracking number prefix to add (required), notify customer (boolean)
Script
{% if event.preview %}
  {% assign fulfillment = hash %}
  {% assign fulfillment["tracking_number"] = "1234567890" %}
  {% assign fulfillment["admin_graphql_api_id"] = "gid://shopify/Fulfillment/1234567890" %}
{% endif %}

{% if fulfillment.tracking_number == blank %}
  {"log": "This fullfilment has no tracking number; can't add a prefix"}
{% else %}
  {% action "shopify" %}
    mutation {
      fulfillmentTrackingInfoUpdate(
        fulfillmentId: {{ fulfillment.admin_graphql_api_id | json }}
        trackingInfoUpdateInput: {
          trackingDetails: {
            number: {{ fulfillment.tracking_number | prepend: options.tracking_number_prefix_to_add__required | json }}
          }
          notifyCustomer: {{ options.notify_customer__boolean | json }}
        }
      ) {
        fulfillment {
          trackingInfo {
            number
          }
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.