Email vendors when their products are ordered, with Mechanic.

Mechanic is a 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 %}
    {% capture order_json %}
      {
        "line_items": [
          {
            "id": 4776535457853,
            "title": "Test line item",
            "quantity": 5,
            "sku": "ABC123",
            "vendor": {{ options.vendors_and_email_addresses__keyval_required.first.first | json }},
            "variant_title": "Test variant title"
          }
        ]
      }
    {% endcapture %}

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

  {% assign order_vendors = order.line_items | map: "vendor" | uniq %}

  {% for vendor in order_vendors %}
    {% if vendor == blank %}
      {% continue %}
    {% endif %}

    {% assign vendor_email = options.vendors_and_email_addresses__keyval_required[vendor] %}

    {% if vendor_email == blank %}
      {% log message: "No email address found for vendor; skipping", vendor: vendor %}
      {% continue %}
    {% endif %}

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

    {% assign order_data = order | json | parse_json %}
    {% assign order_data["line_items"] = array %}

    {% for line_item in vendor_line_items %}
      {% comment %}
        Augment the line item that we'll send with the email event, with any additional data your email needs.
      {% endcomment %}
      {% assign line_item_data = line_item | json | parse_json %}
      {% assign line_item_data["variant_barcode"] = line_item.variant.barcode %}
      {% assign order_data["line_items"][forloop.index0] = line_item_data %}
    {% endfor %}

    {% action "event" %}
      {
        "topic": "user/orders/send_vendor_email",
        "task_id": {{ task.id | json }},
        "data": {
          "vendor": {{ vendor | json }},
          "vendor_email": {{ vendor_email | json }},
          "order": {{ order_data | 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
Vendors and email addresses
{"Vendor X"=>"vendor-x@example.com"}
Email subject
New order: {{ event.data.order.name }} from {{ shop.name }}
Email body
{% assign order = event.data.order %}

<p>Hello,</p>

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

<ul>
{% for line_item in order.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 order.shipping_address %}
{{ order.shipping_address.name }}
{% if order.shipping_address.company != blank %}<br>{{ order.shipping_address.company }}{% endif %}
<br>{{ order.shipping_address.address1 }}
{% if order.shipping_address.address2 != blank %}<br>{{ order.shipping_address.address2 }}{% endif %}
<br>{{ order.shipping_address.city }}, {{ order.shipping_address.province }}
<br>{{ order.shipping_address.zip }} {{ order.shipping_address.country_code }}
{% else %}
(missing shipping address)
{% endif %}
</p>

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