Abandoned checkout emails, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Abandoned checkout emails

by Isaac Bowen (team@usemechanic.com)

Roll your own abandoned checkout emails with this task – or, modify it to perform any other actions that your business needs when a checkout is let go. :) Out of the box, this task sends customers a recovery link, with an emails subject and message that you can easily customize.

Runs 24 hours after user/checkouts/abandoned is triggered, when a checkout is created, when a checkout is updated, and when an order is created. Configuration includes email subject, email body, and hours to wait before sending email.

15-day free trial – unlimited tasks

Documentation

This task monitors checkouts in your store, and kicks off an email a day after a checkout is created – if the checkout wasn't completed, and if the customer provided their email address.

To change the followup time from 1 day to something else, click the "Show Advanced" link, and tweak the subscriptions with the time interval that suits your business.

Please note! While the full checkout details are stored, it'll require some custom code to substitute in more details than the checkout recovery URL. Click the "Show Advanced" link in the task editor to dig into the task script, and feel free to message support if you have any questions.

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
24 hours after user/checkouts/abandoned is triggered (user/checkouts/abandoned+24.hours)
when a checkout is created (shopify/checkouts/create)
when a checkout is updated (shopify/checkouts/update)
when an order is created (shopify/orders/create)
Options
email subject (required), email body (required, multiline), hours to wait before sending email (number, required)
Script
{% assign abandoned_event = "user/checkouts/abandoned" %}

{% if event.topic == "shopify/checkouts/create" or event.topic == "shopify/checkouts/update" %}

  {% if event.preview %}
    {% assign checkout = hash %}
    {% assign checkout["token"] = "00000000000000000000000000000000" %}
    {% assign checkout["abandoned_checkout_url"] = "https://example.shop/checkouts/..." %}
  {% endif %}

  {% comment %}
    When a checkout is created or updated, save it to the cache.
  {% endcomment %}

  {% capture checkout_cache_key %}checkout:{{ checkout.token }}{% endcapture %}

  {% action "cache", "set", checkout_cache_key, checkout %}

  {% if event.topic == "shopify/checkouts/create" %}
    {% comment %}
      ... and if we're looking at the create event, schedule our abandoned checkout followup.
    {% endcomment %}

    {% action "event" %}
      {
        "topic": {{ abandoned_event | json }},
        "data": {
          "checkout_token": {{ checkout.token | json }}
        }
      }
    {% endaction %}

  {% endif %}

{% elsif event.topic == "shopify/orders/create" %}

  {% if event.preview %}
    {% assign order = hash %}
    {% assign order["name"] = "#1234" %}
    {% assign order["checkout_token"] = "00000000000000000000000000000000" %}
  {% endif %}

  {% comment %}
    When an order comes in, cache the order name associated with the checkout token. We'll use this
    to determine if a checkout made it all the way to an order, thus making the checkout *not* abandoned.
  {% endcomment %}

  {% if order.checkout_token != blank %}
    {% capture order_name_cache_key %}checkout_order_name:{{ order.checkout_token }}{% endcapture %}

    {% action "cache", "set", order_name_cache_key, order.name %}
  {% endif %}

{% elsif event.topic == abandoned_event %}

  {% comment %}
    At this stage, our job is to check the cache and see if the checkout converted. If it did, we
    do nothing. If it didn't, and we have checkout data safely in the cache, we fire off an email.
    If somehow the checkout didn't convert but also didn't make it to the cache, we bail.
  {% endcomment %}

  {% assign checkout_token = event.data.checkout_token %}

  {% capture order_name_cache_key %}checkout_order_name:{{ checkout_token }}{% endcapture %}
  {% capture checkout_cache_key %}checkout:{{ checkout_token }}{% endcapture %}

  {% comment %}
    Make sure to render an email during event preview - Mechanic and the merchant both need to
    see what this email will look like.
  {% endcomment %}

  {% if cache[order_name_cache_key] and event.preview != true %}
    {% capture message %}Checkout converted as order {{ cache[order_name_cache_key] }} - not sending an email{% endcapture %}
    {% log message %}
  {% elsif cache[checkout_cache_key] != blank or event.preview %}
    {% assign checkout = cache[checkout_cache_key] %}

    {% action "email" %}
      {
        "to": {{ checkout.email | json }},
        "subject": {{ options.email_subject__required | json }},
        "body": {{ options.email_body__required_multiline | replace: 'ABANDONED_CHECKOUT_URL', checkout.abandoned_checkout_url | strip | newline_to_br | json }},
        "reply_to": {{ shop.customer_email | json }},
        "from_display_name": {{ shop.name | json }}
      }
    {% endaction %}
  {% else %}
    {% log "Checkout did not convert to an order, but no email address was ever given - unable to send an email" %}
  {% endif %}

{% 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
Just one more step to finish your order!
Email body
Hey there! You're almost done:

<a href="ABANDONED_CHECKOUT_URL">Finish your order today!</a>

Thanks,
- The team at {{ shop.name }}
Hours to wait before sending email
24