Email vendors when their products are ordered, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Email vendors when their products are ordered

by Isaac Bowen (team@usemechanic.com)

Use this task to notify vendors when items of theirs have been purchased, by providing this task with a list of vendor names, and the related email addresses.

Runs when an order is created and when user/orders/send_vendor_email is triggered. Configuration includes vendors and email addresses, email subject, and email body.

15-day free trial – unlimited tasks

Documentation

Configure the "Vendors and email addresses" option using vendor names on the left, and their email addresses on the right. Vendor names are case-sensitive!

Customize the email subject and body to taste. Use the default email body as a reference for making sure that you only show relevant line items in each vendor's email message.

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 user/orders/send_vendor_email is triggered (user/orders/send_vendor_email)
Options
vendors and email addresses (keyval, required), email subject (required), email body (required, multiline)
Script
{% if event.topic contains "shopify/orders/" %}
  {% if event.preview %}
    {% assign order_vendors = array %}
    {% assign order_vendors[0] = options.vendors_and_email_addresses__keyval_required.first.first %}
  {% else %}
    {% assign order_vendors = order.line_items | map: "vendor" | uniq %}
  {% endif %}

  {% for vendor in order_vendors %}
    {% assign vendor_email = options.vendors_and_email_addresses__keyval_required[vendor] %}
    {% if vendor_email == blank %}
      {"log": {{ "No email address found for vendor " | append: vendor | json }}}
      {% continue %}
    {% endif %}

    {% assign vendor_line_items = order.line_items | where: "vendor", vendor %}

    {% action "event" %}
      {
        "topic": "user/orders/send_vendor_email",
        "task_id": {{ task.id | json }},
        "data": {
          "vendor": {{ vendor | json }},
          "vendor_email": {{ vendor_email | json }},
          "order": {{ order | json }}
        }
      }
    {% endaction %}
  {% endfor %}
{% elsif event.topic == "user/orders/send_vendor_email" %}
  {% action "email" %}
    {
      "to": {{ event.data.vendor_email | json }},
      "subject": {{ options.email_subject__required | json }},
      "body": {{ options.email_body__required_multiline | 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
Email subject
New order: {{ event.data.order.name }} from {{ shop.name }}
Email body
{% assign o = event.data.order %}

<p>Hello,</p>

<p>A new order has arrived ({{ o.name }}) for the following items:</p>

{% assign line_items = o.line_items | where: "vendor", event.data.vendor %}
<ul>
{% for line_item in line_items %}
<li>{{ line_item.quantity }}x {% if line_item.sku %}{{ line_item.sku }} -{% endif %} {{ line_item.title }} {% if line_item.variant_title != blank %}({{ line_item.variant_title }}){% endif %}
</li>
{% endfor %}
</ul>

<p>The order is to be shipped to the following address:</p>

<p>
{% if o.shipping_address %}
{{ o.shipping_address.name }}
{% if o.shipping_address.company != blank %}<br>{{ o.shipping_address.company }}{% endif %}
<br>{{ o.shipping_address.address1 }}
{% if o.shipping_address.address2 != blank %}<br>{{ o.shipping_address.address2 }}{% endif %}
<br>{{ o.shipping_address.city }}, {{ o.shipping_address.province }}
<br>{{ o.shipping_address.zip }} {{ o.shipping_address.country_code }}
{% else %}
(missing shipping address)
{% endif %}
</p>

<p>
Thanks,
<br>{{ shop.name }}
</p>