Mechanic Get Mechanic
Task library

Email vendors when an order shipping address changes

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

Try this task

When it runs

Occurs whenever an order is updated

What you choose

  • Vendors and email addresses
  • Email body
  • Email subject

Documentation

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

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.

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

shopify/orders/updated
How task subscriptions work

Option types and flags

Task code

{% 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 Liquid reference

Default option values

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 }}