Email the customer when tracking numbers are added to their order, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Email the customer when tracking numbers are added to their order

by Isaac Bowen (team@usemechanic.com)

Use this task with services that add tracking numbers to fulfillments after fulfillments are created, to ensure that customers are notified when their tracking number is available.

Runs when a fulfillment is created and when a fulfillment is updated. Configuration includes email subject and email body.

15-day free trial – unlimited tasks

Documentation

This task works by monitoring created and updated fulfillments. It scans for and saves the tracking numbers currently available for an order, and whenever that set changes, it sends an email to the customer.

Use "TRACKING_NUMBERS" in your email body to insert a list of tracking numbers for the customer's order. Or, use "{{ fulfillment.tracking_number }}", to insert the single tracking number of the fulfillment that the task has just been created or updated.

YouTube: Watch the development video!

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 a fulfillment is created (shopify/fulfillments/create)
when a fulfillment is updated (shopify/fulfillments/update)
Options
email subject (required), email body (required, multiline)
Script
{% assign existing_metafield = fulfillment.order.metafields.mechanic | where: "key", "known_tracking_numbers" | first %}

{% if event.preview %}
  {% capture fulfillment_json %}
    {
      "order": {
        "email": "customer@example.com",
        "metafields": {
          "mechanic": {
            "known_tracking_numbers": []
          }
        },
        "fulfillments": [
          {
            "tracking_numbers": ["ABC123"]
          }
        ]
      }
    }
  {% endcapture %}

  {% assign fulfillment = fulfillment_json | parse_json %}
            
  {% assign existing_metafield = hash %}
  {% assign existing_metafield["id"] = 1234567890 %}
{% endif %}
            
{% assign already_known_tracking_numbers = fulfillment.order.metafields.mechanic.known_tracking_numbers | default: array %}

{% assign currently_known_tracking_numbers = array %}
{% for some_fulfillment in fulfillment.order.fulfillments %}
  {% assign currently_known_tracking_numbers = currently_known_tracking_numbers | concat: some_fulfillment.tracking_numbers %}
{% endfor %}

{% assign tracking_numbers_changed = false %}
{% for tracking_number in currently_known_tracking_numbers %}
  {% unless already_known_tracking_numbers contains tracking_number %}
    {% assign tracking_numbers_changed = true %}
    {% break %}
  {% endunless %}
{% endfor %}

{% if tracking_numbers_changed or existing_metafield == nil %}
  {% if existing_metafield %}
    {% if fulfillment.order.email == blank %}
      {% log "No email on file for this order." %}
    {% else %}
      {% assign tracking_numbers_string = currently_known_tracking_numbers | join: ", " %}
      {% action "email" %}
        {
          "to": {{ fulfillment.order.email | json }},
          "subject": {{ options.email_subject__required | json }},
          "body": {{ options.email_body__required_multiline | replace: "TRACKING_NUMBERS", tracking_numbers_string | strip | newline_to_br | json }},
          "reply_to": {{ shop.customer_email | json }},
          "from_display_name": {{ shop.name | json }}
        }
      {% endaction %}
    {% endif %}
  {% endif %}

  {% action "shopify" %}
    [
      "update",
      [
        "order",
        {{ fulfillment.order.id | json }}
      ],
      {
        "metafields": [
          {
            {% if existing_metafield %}"id": {{ existing_metafield.id | json }},{% endif %}
            "namespace": "mechanic",
            "key": "known_tracking_numbers",
            "value": {{ currently_known_tracking_numbers | json | json }},
            "value_type": "json_string"
          }
        ]
      }
    ]
  {% endaction %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Email subject
Shipping update for order {{ fulfillment.order.name | default: "(number)" }}
Email body
Hello customer,

Track your package: TRACKING_NUMBERS

Thanks,
{{ shop.name }}