Mechanic Get Mechanic
Task library

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.

Try this task

When it runs

Occurs whenever an order is fulfilled

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

shopify/orders/fulfilled
How task subscriptions work

Task 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 %}
Mechanic Liquid reference