Auto-tag fulfilled orders with their carriers, with Mechanic.

Mechanic is a development platform for Shopify. :)

Auto-tag fulfilled orders with their carriers

by Isaac Bowen (team@usemechanic.com)

When an order is fulfilled, this task will scan the tracking information for each fulfillment. The name of each tracking company on file – i.e. the fulfillment carrier – will then be added to the order as a tag.

Runs when an order is fulfilled.

15-day free trial – unlimited tasks

Documentation

When an order is fulfilled, this task will scan the tracking information for each fulfillment. The name of each tracking company on file – i.e. the fulfillment carrier – will then be added to the order as a tag.

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 an order is fulfilled (shopify/orders/fulfilled)
Script
{% if event.preview %}
  {% assign order = hash %}
  {% assign order["admin_graphql_api_id"] = "gid://shopify/Order/1234567890" %}
{% endif %}

{% capture query %}
  query {
    order(id: {{ order.admin_graphql_api_id | json }}) {
      id
      tags
      fulfillments {
        name
        trackingInfo {
          company
        }
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "order": {
          "id": "gid://shopify/Order/1234567890",
          "tags": [],
          "fulfillments": [
            {
              "name": "#1234-F1",
              "trackingInfo": [
                {
                  "company": "Example Logistics 1"
                }
              ]
            },
            {
              "name": "#1234-F2",
              "trackingInfo": [
                {
                  "company": null
                }
              ]
            }
          ]
        }
      }
    }
  {% endcapture %}

  {% assign result = result_json | parse_json %}
{% endif %}

{% assign companies = result.data.order.fulfillments | map: "trackingInfo" | map: "company" %}
{% assign tags_to_add = array %}

{% for company in companies %}
  {% if company == blank or result.data.order.tags contains company %}
    {% continue %}
  {% endif %}

  {% assign tags_to_add[tags_to_add.size] = company %}
{% endfor %}

{% if tags_to_add != empty %}
  {% action "shopify" %}
    mutation {
      tagsAdd(
        id: {{ order.admin_graphql_api_id | json }}
        tags: {{ tags_to_add | json }}
      ) {
        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.