Email vendors when an order shipping address changes with Mechanic.

Mechanic is the one-tool-does-it-all automation app for Shopify. :)

Email vendors when an order shipping address changes

by Isaac Bowen (team@usemechanic.com)

Keep customers happy by making sure your vendors have up-to-date shipping information. This keeps your vendors happy, too!

Runs when an order is created and when an order is updated. Configuration includes vendor email configuration, email subject, and email body.

15-day free trial – unlimited tasks

Documentation

When an order is created, this task will store a temporary copy of the order's shipping address.

When an order is updated, this task will check for a stored copy of the shipping address. If the stored address is different than the updated order address, this task will send an email to any vendors responsible for line items in the order, as long as the vendor email is configured in this task.

Configure this task with a list of vendor names and email addresses, separated by colons, with one vendor per line. Vendor names are case sensitive!

Here's an example:

Nike:nike@example.com
Apple:apple@example.com

Please note: The task can only send emails for orders that it gets to see created initially, or for orders that have two updates.

This is because Shopify doesn't tell Mechanic what changed about an order, only that it's changed somehow. Therefore, Mechanic needs to see what the order shipping address is at least once, so as to compare it to incoming updates.

This task will start sending emails for orders that are created after the task was turned on. To see if it works right away, just try changing some simple about an order (like the order note), waiting a minute, and then try changing the order address. This will give Mechanic a chance to see what the order address was originally, allowing it to be able to determine that the address has changed.

Developer details

Events
when an order is created (shopify/orders/create)
when an order is updated (shopify/orders/updated)
Options
vendor email configuration (required, multiline), email subject (required), email body (required, multiline)
Script
{% capture newline %}
{% endcapture %}

{% assign vendor_email_pairs = options.vendor_email_configuration__required_multiline | split: newline %}

{% capture cache_key %}order-address-{{ order.id }}{% endcapture %}

{% if event.topic == "shopify/orders/updated" %}
  {% if event.preview %}
    {
      "action": {
        "type": "email",
        "options": {
          "to": "vendor@example.com",
          "subject": {{ options.email_subject__required | json }},
          "body": {{ options.email_body__required_multiline | newline_to_br | json }},
          "reply_to": {{ shop.customer_email | json }},
          "from_display_name": {{ shop.name | json }}
        }
      }
    }
  {% else %}
    {% if cache[cache_key] == nil %}
      {"log": "No cached address on file for this order; not sending an email"}
    {% else %}
      {% if order.shipping_address != cache[cache_key] %}
        {% assign vendor_email_sent = false %}

        {% assign order_vendors = order.line_items | map: "vendor" | uniq %}
        {% for order_vendor in order_vendors %}
          {% for vendor_email_pair in vendor_email_pairs %}
            {% assign vendor_email_pair_array = vendor_email_pair | split: ":" %}
            {% if vendor_email_pair_array.first != order_vendor %}
              {% continue %}
            {% endif %}

            {% assign vendor_email_sent = true %}

            {
              "action": {
                "type": "email",
                "options": {
                  "to": {{ vendor_email_pair_array.last | json }},
                  "subject": {{ options.email_subject__required | json }},
                  "body": {{ options.email_body__required_multiline | newline_to_br | json }},
                  "reply_to": {{ shop.customer_email | json }},
                  "from_display_name": {{ shop.name | json }}
                }
              }
            }
          {% endfor %}
        {% endfor %}

        {% if vendor_email_sent == false %}
          {"log": "Address changed, but no matching vendor/email pair was found; not sending an email"}
        {% endif %}
      {% endif %}
    {% else %}
      {"log": "Address did not change; not sending an email"}
    {% endif %}
  {% endif %}
{% endif %}

{% if cache[cache_key] == nil or cache[cache_key] != order.shipping_address or event.preview %}
  {
    "action": {
      "type": "cache",
      "options": {
        "set": {
          "key": {{ cache_key | json }},
          "value": {{ order.shipping_address | json }}
        }
      }
    }
  }
{% endif %}
Yep, that’s just Liquid! If this is interesting to you, check out our documentation! Mechanic is hugely developer-friendly. :)
Defaults
Vendor email configuration
Nike:nike@example.com
Apple:apple@example.com
Email subject
{{ shop.name }}: Shipping address update for {{ order.name }}
Email body
Hello,

Order {{ order.name }} has a new shipping address:

Name: {{ order.shipping_address.first_name }} {{ order.shipping_address.last_name }}
Company: {{ order.shipping_address.company }}
Phone: {{ order.shipping_address.phone }}

{{ order.shipping_address.address1 }}
{{ order.shipping_address.address2 }}
{{ order.shipping_address.city }}, {{ order.shipping_address.province_code }} {{ order.shipping_address.zip }}
{{ order.shipping_address.country_code }}

Thanks,

{{ shop.name }}