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

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

Email the customer when tracking numbers are added to their order

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 Occurs whenever a fulfillment is created and Occurs whenever a fulfillment is updated. Configuration includes email subject and email body.

15-day free trial – unlimited tasks

Documentation

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.

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 has just been created or updated.

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/fulfillments/create
shopify/fulfillments/update
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
email subject (required) , email body (required, multiline)
Code
{% assign email_subject = options.email_subject__required %}
{% assign email_body = options.email_body__required_multiline %}

{% comment %}
  -- get order fulfillment data and any previously recorded tracking numbers
{% endcomment %}

{% capture query %}
  query {
    order(id: {{ fulfillment.order.admin_graphql_api_id | json }}) {
      id
      email
      metafield(key: "mechanic.known_tracking_numbers") {
        jsonValue
      }
      fulfillments {
        id
        trackingInfo {
          number
          url
        }
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "order": {
          "id": "gid://shopify/Order/1234567890",
          "email": "sample@example.com",
          "fulfillments": [
            {
              "trackingInfo": [
                {
                  "number": "1234567890",
                  "url": null
                }
              ]
            }
          ]
        }
      }
    }
  {% endcapture %}

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

{% assign order = result.data.order %}
{% assign already_known_tracking_numbers = order.metafield.jsonValue | default: array %}

{% assign currently_known_tracking_numbers = array %}
{% assign tracking_numbers_changed = nil %}

{% comment %}
  -- check to see if any of the tracking numbers are new
{% endcomment %}

{% for fulfillment in order.fulfillments %}
  {% for tracking in fulfillment.trackingInfo %}
    {% if tracking.number != blank %}
      {% assign currently_known_tracking_numbers = currently_known_tracking_numbers | push: tracking.number %}

      {% unless already_known_tracking_numbers contains tracking.number %}
        {% assign tracking_numbers_changed = true %}
      {% endunless %}
    {% endif %}
  {% endfor %}
{% endfor %}

{% comment %}
  -- if there are changed tracking numnbers, then update known metafield and send customer email
{% endcomment %}

{% if tracking_numbers_changed %}
  {% if order.email == blank %}
    {% log "No email on file for this order." %}

  {% else %}
    {% assign tracking_numbers_string = currently_known_tracking_numbers | join: ", " %}

    {% action "email" %}
      {
        "to": {{ order.email | json }},
        "subject": {{ email_subject | json }},
        "body": {{ email_body | 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 %}

  {% action "shopify" %}
    mutation {
      metafieldsSet(
        metafields: [
          {
            ownerId: {{ order.id | json }}
            namespace: "mechanic"
            key: "known_tracking_numbers"
            type: "json"
            value: {{ currently_known_tracking_numbers | json | json }}
          }
        ]
      ) {
        metafields {
          namespace
          key
          type
          jsonValue
          owner {
            ... on Order {
              id
              name
            }
          }
        }
        userErrors {
          code
          field
          message
        }
      }
    }
  {% endaction %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Email subject
Shipping update for order {{ fulfillment.order.name | default: "(number)" }}
Email body
Hello customer,

Track your package: TRACKING_NUMBERS

Thanks,
{{ shop.name }}