Auto-tag fulfilled orders by carrier, with Mechanic.

Mechanic is a development platform for Shopify. :)

Auto-tag fulfilled orders by carrier

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 be used to look up a new order tag to apply, per the task's configuration.

Runs when an order is fulfilled. Configuration includes fulfillment carriers and order tags.

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 be used to look up a new order tag to apply, per the task's configuration.

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)
Options
fulfillment carriers and order tags (keyval, required)
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": {{ options.fulfillment_carriers_and_order_tags__keyval_required.first.first | json }}
                }
              ]
            }
          ]
        }
      }
    }
  {% endcapture %}

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

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

{% assign tags_to_add = array %}
{% assign order_tags = result.data.order.tags %}

{% for company in companies %}
  {% assign tag = options.fulfillment_carriers_and_order_tags__keyval_required[company] %}

  {% if tag == blank or tags_to_add contains tag or order_tags contains tag %}
    {% continue %}
  {% endif %}

  {% assign tags_to_add[tags_to_add.size] = tag %}
{% 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.
Defaults
Fulfillment carriers and order tags
{"DHL Global"=>"international"}