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

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

Auto-tag fulfilled orders with their carriers

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 Occurs whenever an order is fulfilled.

15-day free trial – unlimited tasks

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