Abandoned cart emails with Mechanic.

Mechanic is the one-tool-does-it-all automation app for Shopify. :)

Abandoned cart emails

by Isaac Bowen (team@usemechanic.com)

Roll your own abandoned cart emails with this task – or, modify it to perform any other actions that your business needs when a cart 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 1 day 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 and email body.

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

Events
1 day after user/checkouts/abandoned is triggered (user/checkouts/abandoned+1.day)
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)
Script
{% assign abandoned_event = "user/checkouts/abandoned" %}

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

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

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

  {
    "action": {
      "type": "cache",
      "options": {
        "set": {
          "key": {{ checkout_cache_key | json }},
          "value": {{ checkout | json }}
        }
      }
    }
  }

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

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

{% elsif event.topic == "shopify/orders/create" and order.checkout_token != blank %}

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

  {% capture order_name_cache_key %}checkout_order_name:{{ order.checkout_token }}{% endcapture %}

  {
    "action": {
      "type": "cache",
      "options": {
        "set": {
          "key": {{ order_name_cache_key | json }},
          "value": {{ order.name | json }}
        }
      }
    }
  }

{% 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 | json }}}
  {% elsif cache[checkout_cache_key] != blank or event.preview %}
    {% assign checkout = cache[checkout_cache_key] %}

    {
      "action": {
        "type": "email",
        "options": {
          "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 }}
        }
      }
    }
  {% else %}
    {"log": "Checkout did not convert to an order, but no email address was ever given - unable to send an email"}
  {% endif %}

{% endif %}
Yep, that’s just Liquid! If this is interesting to you, check out our documentation! Mechanic is hugely developer-friendly. :)
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 }}