Email vendors when an order is tagged, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

Email vendors when an order is tagged

Use this task to automatically keep vendors up to date as an order evolves. Configure this task with a list of known vendor names and their email addresses, and this task will email them whenever it notices the tag of your choice on the order.

Runs Occurs whenever an order is updated. Configuration includes order tag to watch for, vendors and email addresses, email subject, email body, and ignore orders older than this task.

15-day free trial – unlimited tasks

Documentation

Use this task to automatically keep vendors up to date as an order evolves. Configure this task with a list of known vendor names and their email addresses, and this task will email them whenever it notices the tag of your choice on the order.

This task watches for new and updated orders, and sends an email to all known vendors on the order when the tag of your choice is observed. To make a note of which orders have had their emails sent, this task adds another tag. (For example, if this task is configured to watch for the "hold" tag, this task will add the tag "hold-vendor-emails-sent" after sending that order's vendor emails.)

Configure the "Vendors and email addresses" options with vendor names on the left, and vendor email addresses on the right.

Important note: Because Shopify doesn't let apps know what has changed about an order, it's safest to use the "Ignore orders older than this task" option. This way, this task is guaranteed to receive every update for the order since the order is created, allowing it to know for sure when an order has been tagged. Without this option, orders that are already tagged run the risk of having emails sent for them.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, 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.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
shopify/orders/updated
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
order tag to watch for (required), vendors and email addresses (keyval, required), email subject (required), email body (required, multiline), ignore orders older than this task (boolean)
Code
{% comment %}
  An opinion about option order:

  {{ options.order_tag_to_watch_for__required }}
  {{ options.vendors_and_email_addresses__keyval_required }}
  {{ options.email_subject__required }}
  {{ options.email_body__required_multiline }}
  {{ options.ignore_orders_older_than_this_task__boolean }}
{% endcomment %}

{% if event.preview %}
  {% assign order = hash %}
  {% assign order["admin_graphql_api_id"] = "gid://shopify/Order/1234567890" %}
  {% assign order["tags"] = options.order_tag_to_watch_for__required %}
  {% assign order["line_items"] = array %}
  {% assign order["line_items"][0] = hash %}
  {% assign order["line_items"][0]["vendor"] = options.vendors_and_email_addresses__keyval_required.first.first %}
{% endif %}

{% assign emails_sent_tag = options.order_tag_to_watch_for__required | append: "-vendor-emails-sent" %}

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

{% assign order_qualifies = false %}

{% assign order_tags = order.tags | split: ", " %}
{% if order_tags contains options.order_tag_to_watch_for__required %}
  {% if order_tags contains emails_sent_tag %}
    {% log "Emails have already been sent for this order; nothing to do." %}
  {% else %}
    {% assign order_qualifies = true %}
  {% endif %}
{% endif %}

{% unless event.preview %}
  {% if options.ignore_orders_older_than_this_task__boolean %}
    {% assign order_created_at_s = order.created_at | date: "%s" | times: 1 %}
    {% assign task_created_at_s = task.created_at | date: "%s" | times: 1 %}
    {% if order_created_at_s < task_created_at_s %}
      {% assign order_qualifies = false %}
    {% endif %}
  {% endif %}
{% endunless %}

{% if order_qualifies %}
  {% 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 }}{% endlog %}
      {% continue %}
    {% endif %}

    {% action "email" %}
      {
        "to": {{ vendor_email | json }},
        "subject": {{ options.email_subject__required | json }},
        "body": {{ options.email_body__required_multiline | strip | newline_to_br | 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: {{ emails_sent_tag | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endfor %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Vendors and email addresses
{"Apple"=>"apple@example.com", "Nike"=>"nike@example.com"}
Email subject
Order {{ order.name }} has been tagged
Email body
Hello,

Order {{ order.name }} has been tagged.

Thanks,
{{ shop.name }}
Ignore orders older than this task
true