Send a new-order email to someone based on a cart attribute, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Send a new-order email to someone based on a cart attribute

by Isaac Bowen (team@usemechanic.com)

This task sends a new-order email alert to somebody specific, based on the contents of a cart attribute. Useful for allowing customers to select their salesperson, alerting the appropriate salesperson when the order is placed.

Runs when an order is created. Configuration includes cart attribute name, cart attribute values and email addresses, email subject, email body, and only send for paid orders.

15-day free trial – unlimited tasks

Documentation

This task sends a new-order email alert to somebody specific, based on the contents of a cart attribute. If a matching email address is not found in the task's configuration, no email will be sent.

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)
Options
cart attribute name (required), cart attribute values and email addresses (keyval, email, required), email subject (required), email body (multiline, required), only send for paid orders (boolean)
Script
{% if event.preview %}
  {% assign order = order | json | parse_json %}
  {% assign order["note_attributes"][0] = hash %}
  {% assign order["note_attributes"][0]["name"] = options.cart_attribute_name__required %}
  {% assign order["note_attributes"][0]["value"] = options.cart_attribute_values_and_email_addresses__keyval_email_required.first.first %}
{% endif %}

{% assign attribute = order.note_attributes | where: "name", options.cart_attribute_name__required | first %}
{% assign attribute_value = attribute.value %}
{% assign email = options.cart_attribute_values_and_email_addresses__keyval_email_required[attribute_value] %}

{% if attribute == nil %}
  {"log": {{ options.cart_attribute_name__required | json | prepend: "This order did not contain the attribute " |  json }}}
{% elsif email == blank %}
  {"log": {{ attribute_value | json | prepend: "There is no configured email corresponding with " |  json }}}
{% else %}
  {% action "email" %}
    {
      "to": {{ email | json }},
      "subject": {{ options.email_subject__required | json }},
      "body": {{ options.email_body__multiline_required | strip | newline_to_br | json }},
      "reply_to": {{ shop.customer_email | json }},
      "from_display_name": {{ shop.name | json }}
    }
  {% endaction %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Cart attribute name
Salesperson
Cart attribute values and email addresses
{"Jenny "=>"jenny@example.com", "Zhang Wei"=>"zhangwei@example.com"}
Email subject
You've got an order! {{ order.name }}
Email body
Hello,

An order has arrived, and it's assigned to you.

https://{{ shop.domain }}/admin/orders/{{ order.id }}

Thanks,
{{ shop.name }}