Send follow-up emails after sending a draft order invoice, with Mechanic.

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

Send follow-up emails after sending a draft order invoice

Use this task to send a follow-up email, 24 hours after you send out an email invoice for a draft order. Optionally, set this task to continue sending follow-up messages, every day after that.

Runs Occurs whenever user/draft_orders/followup is triggered, with a 24 hour delay and Occurs whenever a draft order is updated. Configuration includes send daily emails, email subject, and email body.

15-day free trial – unlimited tasks

Documentation

Use this task to send a follow-up email, 24 hours after you send out an email invoice for a draft order. Optionally, set this task to continue sending follow-up messages, every day after that.

After you send an invoice for a draft order, this task will send a follow-up email 24 hours later, reminding the customer on file to complete the order.

Please note: Because Shopify does not share information about the invoice you send, this task uses the email address of the customer on file for the order, even if this is different than the email you use to send the invoice. If the draft order does not have a customer on file, the task will not be able to send a follow-up.

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
user/draft_orders/followup+24.hours
shopify/draft_orders/update
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
send daily emails (boolean), email subject (required), email body (multiline, required)
Code
{% comment %}
  This is here to make sure that this option sits at the top of the task form. :)
{% endcomment %}
{% assign _ = options.send_daily_emails__boolean %}

{% if event.topic == "shopify/draft_orders/update" %}
  {% assign five_minutes_ago_seconds = "now" | date: "%s" | divided_by: 1 | minus: 300 %}
  {% assign draft_order_invoice_sent_at_seconds = draft_order.invoice_sent_at | date: "%s" | divided_by: 1 %}
  {% capture draft_order_followup_cache_key %}draft-order-followup-{{ draft_order.id }}{% endcapture %}

  {% comment %}
    This being the draft update event, we only want to trigger the followup
    loop if *all* of the following are true:

    1. The draft has an email on file (because shopify doesn't report to us what
       email was actually used to send the draft previously, making the draft's
       on-file email our only option)
    2. The draft's status is "invoice_sent" (because that's the only status that
       we care about)
    3. If the invoice was actually sent in the last five minutes (to avoid
       triggering the loop for super old drafts that receive an update)
    4. If we haven't cached a flag marking the draft as having gotten follow-up
  {% endcomment %}
  {% assign draft_order_qualifies = false %}
  {% if draft_order.email == blank %}
    {% capture log_message %}No email on file; not sending followup{% endcapture %}
  {% elsif draft_order.status != "invoice_sent" %}
    {% capture log_message %}Draft status is {{ draft_order.status }}, not "invoice_sent"; not sending followup{% endcapture %}
  {% elsif draft_order_invoice_sent_at_seconds <= five_minutes_ago_seconds %}
    {% capture log_message %}Draft was sent more than 5min ago, making that event appear stale; not sending followup{% endcapture %}
  {% elsif cache[draft_order_followup_cache_key] != blank %}
    {% capture log_message %}Mechanic has already set up followup for this draft; not sending followup{% endcapture %}
  {% else %}
    {% assign draft_order_qualifies = true %}
  {% endif %}

  {% if log_message != blank %}
    {"log": {{ log_message | json }}}
  {% endif %}

  {% comment %}
    The only thing we do here is kick off the followup loop, and flag the
    draft in our cache.
  {% endcomment %}
  {% if event.preview or draft_order_qualifies %}
    {
      "action": {
        "type": "event",
        "options": {
          "topic": "user/draft_orders/followup",
          "data": {
            "draft_order_id": {{ draft_order.id | json }}
          }
        }
      }
    }

    {
      "action": {
        "type": "cache",
        "options": {
          "set": {
            "key": {{ draft_order_followup_cache_key | json }},
            "value": true
          }
        }
      }
    }
  {% endif %}
{% elsif event.topic == "user/draft_orders/followup" %}
  {% assign draft_order = shop.draft_orders[event.data.draft_order_id] %}

  {% assign draft_order_qualifies = false %}
  {% if draft_order and draft_order.order_id == blank %}
    {% assign draft_order_qualifies = true %}
  {% endif %}

  {% comment %}
    This is the loop! We send the email, and - if looping is enabled, since
    that's actually optional - we schedule the loop's next run.
  {% endcomment %}
  {% if event.preview or draft_order_qualifies %}
    {
      "action": {
        "type": "email",
        "options": {
          "to": {{ draft_order.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 }}
        }
      }
    }

    {% if options.send_daily_emails__boolean %}
      {
        "action": {
          "type": "event",
          "options": {
            "topic": "user/draft_orders/followup",
            "data": {
              "draft_order_id": {{ draft_order.id | json }}
            }
          }
        }
      }
    {% endif %}
  {% endif %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Send daily emails
true
Email subject
Invoice {{ shop.draft_orders[event.data.draft_order_id].name }}: Your order payment is due
Email body
Hello,

Please complete your order using this link:

{{ shop.draft_orders[event.data.draft_order_id].invoice_url | default: "(invoice url will be displayed here)" }}

Thanks,
The team at {{ shop.name }}