Email vendors when an order shipping address changes, with Mechanic.

Mechanic is an automation development platform 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 vendors and email addresses, email body, and email subject.

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 on the left, and email addresses on the right. Vendor names are case sensitive!


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

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 an order is created (shopify/orders/create)
when an order is updated (shopify/orders/updated)
Options
vendors and email addresses (required, keyval), email body (required, multiline), email subject (required)
Script
{% comment %}
  Preferred option order:

  {{ options.vendors_and_email_addresses__required_keyval }}
  {{ options.email_body__required_multiline }}
  {{ options.email_subject__required }}
{% endcomment %}

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

{% if event.preview %}
  {% capture order_json %}
    {
      "shipping_address": {
        "address1": {{ shop.address1 | json }},
        "address2": {{ shop.address2 | json }},
        "city": {{ shop.city | json }},
        "company": {{ shop.name | json }},
        "country_code": {{ shop.country_code | json }},
        "first_name": {{ shop.shop_owner | split: " " | first | json }},
        "last_name": {{ shop.shop_owner | split: " " | last | json }},
        "phone": {{ shop.phone | json }},
        "province_code": {{ shop.province_code | json }},
        "zip": {{ shop.zip | json }}
      },
      "line_items": [
        {% assign pair = options.vendors_and_email_addresses__required_keyval.first %}

        {
          "vendor": {{ pair[0] | json }}
        }
      ]
    }
  {% endcapture %}

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

{% if event.topic == "shopify/orders/updated" %}
  {% if event.preview != true and cache[cache_key] == nil %}
    {"log": "No cached address on file for this order; not sending an email"}
  {% elsif event.preview != true and order.shipping_address == cache[cache_key] %}
    {"log": "Address did not change; not sending an email"}
  {% else %}
    {% assign order_vendors = order.line_items | map: "vendor" | uniq %}

    {% for order_vendor in order_vendors %}
      {% assign vendor_email_sent = false %}
      {% assign vendor_email = options.vendors_and_email_addresses__required_keyval[order_vendor] %}

      {% if vendor_email != blank %}
        {% assign vendor_email_sent = true %}

        {% action "email" %}
          {
            "to": {{ vendor_email | 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 }}
          }
        {% endaction %}
      {% endif %}

      {% if vendor_email_sent == false %}
        {"log": {{ "Address changed, but no matching email was found for vendor " | append: order_vendor | append: "; not sending an email" | json }}}
      {% endif %}
    {% endfor %}
  {% endif %}
{% endif %}

{% if cache[cache_key] == nil or cache[cache_key] != order.shipping_address or event.preview %}
  {% action "cache", "set", cache_key, order.shipping_address %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Vendors and email addresses
{"Nike"=>"nike@example.com", "Apple"=>"apple@example.com"}
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 }}
Email subject
{{ shop.name }}: Shipping address update for {{ order.name }}