Add fulfillment tracking when an order is tagged, with Mechanic.

Mechanic is a development platform for Shopify. :)

Add fulfillment tracking when an order is tagged

Configure this tag with a mapping of country codes to tracking companies and URL templates, and/or default companies and URL templates. When an order is tagged, this task will apply the appropriate values to each fulfillment that has a tracking number.

Runs when an order is created and when an order is updated. Configuration includes order tag to watch for, country codes and tracking url templates, default tracking url template, overwrite existing tracking urls, country codes and tracking companies, default tracking company, and notify customers.

15-day free trial – unlimited tasks

Documentation

Configure this tag with a mapping of country codes to tracking companies and URL templates, and/or default companies and URL templates. When an order is tagged, this task will apply the appropriate values to each fulfillment that has a tracking number.

Configure this tag with mappings of two-character country codes to tracking companies, and tracking URL templates. Both companies and URL templates support a default value as well, which will be used for orders that don't have a mapped country in the task's configuration. When an order is tagged, this task will apply the appropriate values to each fulfillment that has a tracking number.

If "Notify customers" is enabled, this task will request that Shopify send a notification email when the fulfillment update is processed.

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.

Open source
View on GitHub to contribute to this task
Events
when an order is created (shopify/orders/create)
when an order is updated (shopify/orders/updated)
Options
order tag to watch for (required), country codes and tracking url templates (keyval), default tracking url template, overwrite existing tracking urls (boolean), country codes and tracking companies (keyval), default tracking company, notify customers (boolean)
Script
{% comment %}
  Preferred option order:

  {{ options.order_tag_to_watch_for__required }}
  {{ options.country_codes_and_tracking_url_templates__keyval }}
  {{ options.default_tracking_url_template }}
  {{ options.overwrite_existing_tracking_urls__boolean }}
  {{ options.country_codes_and_tracking_companies__keyval }}
  {{ options.default_tracking_company }}
  {{ options.notify_customers__boolean }}
{% endcomment %}

{% if options.country_codes_and_tracking_url_templates__keyval == blank and options.default_tracking_url_template == blank %}
  {% error "Fill in at least one of 'Country codes and tracking URL templates' and 'Fallback tracking URL template'. :)" %}
{% endif %}

{% for pair in options.country_codes_and_tracking_url_templates__keyval %}
  {% unless pair[1] contains "TRACKING_NUMBER" %}
    {% error %}{{ "The template for " | append: pair[0] | append: " does not contain the placeholder 'TRACKING_NUMBER'. Please add it!" | json }}{% enderror %}
  {% endunless %}
{% endfor %}

{% if options.default_tracking_url_template != blank %}
  {% unless options.default_tracking_url_template contains "TRACKING_NUMBER" %}
    {% error %}{{ "The default template for does not contain the placeholder 'TRACKING_NUMBER'. Please add it!" | json }}{% enderror %}
  {% endunless %}
{% endif %}

{% if event.preview %}
  {% capture order_json %}
    {
      "tags": {{ options.order_tag_to_watch_for__required | json }},
      "shipping_address": {
        "country_code": {{ options.country_codes_and_tracking_url_templates__keyval.first.first | json }}
      },
      "fulfillments": [
        {
          "admin_graphql_api_id": "gid://shopify/Fulfillment/1234567890",
          "name": "#1234.1",
          "tracking_company": "Other",
          "tracking_number": "ABCDEF123456",
          "tracking_url": null
        }
      ]
    }
  {% endcapture %}

  {% assign order = order_json | parse_json %}
{% endif %}

{% assign order_tags = order.tags | split: ", " %}
{% if order_tags contains options.order_tag_to_watch_for__required %}
  {% assign template = options.country_codes_and_tracking_url_templates__keyval[order.shipping_address.country_code] %}
  {% assign template = template | default: options.default_tracking_url_template %}

  {% for fulfillment in order.fulfillments %}
    {% if fulfillment.tracking_number == blank or fulfillment.tracking_number == "false" %}
      {% continue %}
    {% endif %}

    {% assign new_fulfillment_tracking_url = template | replace: "TRACKING_NUMBER", fulfillment.tracking_number %}
    {% assign new_fulfillment_tracking_company = options.country_codes_and_tracking_companies__keyval[order.shipping_address.country_code] | default: options.default_tracking_company | default: fulfillment.tracking_company %}

    {% if options.overwrite_existing_tracking_urls__boolean == false and fulfillment.tracking_url != blank %}
      {% log message: "Fulfillment already has a tracking URL, and this task is configured not to overwrite, so we're sticking with the existing tracking URL", new_fulfillment_tracking_url: new_fulfillment_tracking_url, fulfillment_tracking_url: fulfillment.tracking_url, fulfillment_name: fulfillment.name, fulfillment_id: fulfillment.id %}
      {% assign new_fulfillment_tracking_url = fulfillment.tracking_url %}
    {% endif %}

    {% if new_fulfillment_tracking_url == fulfillment.tracking_url and new_fulfillment_tracking_company == fulfillment.tracking_company %}
      {% log message: "Tracking URL and company is unchanged; skipping this fulfillment", fulfillment_tracking_company: fulfillment.tracking_company, fulfillment_tracking_url: fulfillment.tracking_url, fulfillment_name: fulfillment.name, fulfillment_id: fulfillment.id %}
    {% else %}
      {% action "shopify" %}
        mutation {
          fulfillmentTrackingInfoUpdateV2(
            fulfillmentId: {{ fulfillment.admin_graphql_api_id | json }}
            trackingInfoInput: {
              company: {{ new_fulfillment_tracking_company | json }}
              number: {{ fulfillment.tracking_number | json }}
              url: {{ new_fulfillment_tracking_url | json }}
            }
            notifyCustomer: {{ options.notify_customers__boolean | json }}
          ) {
            fulfillment {
              trackingInfo {
                company
                number
                url
              }
            }
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Order tag to watch for
update-tracking
Country codes and tracking url templates
{"US"=>"https://tools.usps.com/go/TrackConfirmAction_input?qtc_tLabels1=TRACKING_NUMBER", "CA"=>"https://www.canadapost.ca/trackweb/en#/search?searchFor=TRACKING_NUMBER", "AU"=>"https://auspost.com.au/mypost/track/#/details/TRACKING_NUMBER", "NZ"=>"https://www.nzpost.co.nz/tools/tracking/item/TRACKING_NUMBER", "GB"=>"http://www.royalmail.com/portal/rm/track?trackNumber=TRACKING_NUMBER"}
Default tracking url template
http://parcelsapp.com/en/tracking/TRACKING_NUMBER
Overwrite existing tracking urls
true
Country codes and tracking companies
{"US"=>"USPS", "CA"=>"Canada Post", "AU"=>"Australia Post", "NZ"=>"New Zealand Post", "GB"=>"Royal Mail"}
Default tracking company
Parcels
Notify customers
true