Send email when an order is tagged, with Mechanic.

Mechanic is a development platform for Shopify. :)

Send email when an order is tagged

by Isaac Bowen (team@usemechanic.com)

Use this task to monitor order tags, and notify staff, vendors, or any other interested parties when an order receives a certain tag. Useful for keeping teams across your business up to date as an order progresses.

Runs when an order is created and when an order is updated. Configuration includes tag to watch for, ignore orders that are older than this task, email recipients, email subject, and email body.

15-day free trial – unlimited tasks

Documentation

This task monitors incoming orders, and updates to existing orders, looking for the tag of your choice. When the tag is detected, this task will send an email to the address(es) of your choice.

Important notes

  • This task also adds a second tag to the order when the email is sent, as a flag to mark the order as having had that email sent. This helps Mechanic remember what orders should not have an email sent.
  • To help out merchants who have lots of existing orders, by default this task doesn't watch orders that were created before you add this task to your Mechanic account.

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
tag to watch for (required), ignore orders that are older than this task (boolean), email recipients (array, required), email subject (required), email body (required, multiline)
Script
{% comment %}
  Using a default value here, because an unsaved task doesn't yet have a created_at
  value, and we definitely need one for rendering preview actions.
{% endcomment %}
{% assign task_created_at = task.created_at | default: "2020-01-01" | date: "%s" %}

{% if event.preview %}
  {% assign order = hash %}
  {% assign order["admin_graphql_api_id"] = "gid://shopify/Order/1234567890" %}
  {% assign order["tags"] = options.tag_to_watch_for__required %}
  {% assign order["created_at"] = task_created_at | date: "%s" %}
{% endif %}

{% capture email_sent_tag %}{{ options.tag_to_watch_for__required }}-email-sent{% endcapture %}
{% assign order_tags = order.tags | split: ", " %}

{% assign order_qualifies = false %}
{% assign order_created_at = order.created_at | date: "%s" %}

{% if options.ignore_orders_that_are_older_than_this_task__boolean and order_created_at < task_created_at %}
  {% log "This order was created before this task was added to the store. Per the configured policy of this task, in order to avoid accidental emails for old and previously-tagged orders, this order will be ignored." %}
{% elsif order_tags contains options.tag_to_watch_for__required %}
  {% unless order_tags contains email_sent_tag %}
    {% assign order_qualifies = true %}
  {% endunless %}
{% endif %}

{% assign email_recipients = options.email_recipients__array_required %}
{% assign email_subject = options.email_subject__required %}
{% assign email_body = options.email_body__required_multiline | strip | newline_to_br %}

{% if order_qualifies %}
  {% action "email" %}
    {
      "to": {{ email_recipients | join: ", " | json }},
      "subject": {{ email_subject | json }},
      "body": {{ email_body | json }},
      "reply_to": {{ shop.customer_email | json }},
      "from_display_name": {{ shop.name | json }}
    }
  {% endaction %}

  {% action "shopify" %}
    mutation {
      tagsAdd(
        id: {{ order.admin_graphql_api_id | json }}
        tags: {{ email_sent_tag | json }}
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Tag to watch for
in-production
Ignore orders that are older than this task
true
Email recipients
["production+team@example.com", "support+team@example.com"]
Email subject
[{{shop.name}}] Order {{ order.name }}{% if order.customer.name %} placed by {{ order.customer.name }}{% endif %}
Email body
Hello,

{{ order.customer.name | default: 'Someone' }} placed a new order with your store, on {{ order.created_at | date: "%b %d at %I:%M%P" }}:
<ul>  {% for line in order.line_items %} <li> {{ line.quantity }}x {{ line.title }} {% if line.sku != "" %}(SKU: {{line.sku}}){% endif %} for {{ line.price | times: 100 | money }} each </li> {% endfor %} </ul>
Subtotal: {{ order.subtotal_price | times: 100 | money }}
Total: {{ order.total_price | times: 100 | money }}
{% if order.note != blank %}
Note:
{{ order.note }}
{% endif %}

<a href="https://{{ shop.domain }}/admin/orders/{{ order.id }}">View order {{ order.name }}</a>
{% if order.shipping_address %}
<b>Shipping address:</b>
{{ order.shipping_address.first_name }} {{ order.shipping_address.last_name }}
{{ order.shipping_address.address1 }}
{{ order.shipping_address.city }}, {{ shipping_address.province }}  {{ shipping_address.zip }}
{{ order.shipping_address.country }}
{{ order.shipping_address.phone }}
{% endif %}
Thanks,
- Mechanic, for {{ shop.name }}