Mechanic Get Mechanic
Task library

Send your customers reorder reminders

Useful for customers who need recurring reminders to reorder. Optionally filtering by a customer tag, it monitors the most recent order for each customer, and sends them reminders every x days since that order.

Try this task

When it runs

  • Occurs every day at midnight (in local time)
  • Occurs when a bulk operation is completed

What you choose

  • Only include customers having this tag
  • Number of days to wait between emails
  • Email subject
  • Email body
  • Test mode

Documentation

Useful for customers who need recurring reminders to reorder. Optionally filtering by a customer tag, it monitors the most recent order for each customer, and sends them reminders every x days since that order.

This task runs daily, at midnight in your store's timezone. This task will send emails to the address on file for the customer, falling back to the email on file for the order if the customer has no email address.

To test this task, enable test mode, and save. You'll find a new "Run task" button, which will report the emails that would be sent on the current date.

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

{% if options.test_mode__boolean %}
  mechanic/user/trigger
{% endif %}
mechanic/scheduler/daily
mechanic/shopify/bulk_operation
How task subscriptions work

Option types and flags

Task code

{% assign only_include_customers_having_this_tag = options.only_include_customers_having_this_tag %}
{% assign interval_d = options.number_of_days_to_wait_between_emails__number_required %}
{% assign email_subject = options.email_subject__required %}
{% assign email_body = options.email_body__multiline_required %}
{% assign test_mode = options.test_mode__boolean %}

{%- capture cutoff_days -%}
  -{{ interval_d }} days
{%- endcapture -%}
{% assign cutoff_date = "now" | date: "%F", advance: cutoff_days %}

{% if event.topic contains "mechanic/scheduler/" or event.topic == "mechanic/user/trigger" %}
  {%- capture search_query -%}
    last_order_date <= {{ cutoff_date }}
    {%- if only_include_customers_having_this_tag != blank %} AND customer_tags CONTAINS '{{ only_include_customers_having_this_tag }}'{%- endif -%}
  {%- endcapture -%}

  {% log customers_search_query: search_query %}

  {% capture bulk_operation_query %}
    query {
      customerSegmentMembers(
        query: {{ search_query | json }}
      ) {
        edges {
          node {
            __typename
            id
            defaultEmailAddress {
              emailAddress
            }
            lastOrderId
          }
        }
      }
    }
  {% endcapture %}

  {% action "shopify" %}
    mutation {
      bulkOperationRunQuery(
        query: {{ bulk_operation_query | json }}
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}

{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
  {% if event.preview %}
    {% capture jsonl_string %}
      {"__typename":"CustomerSegmentMember","id":"gid://shopify/CustomerSegmentMember/1234567890","lastOrderId":"gid://shopify/Order/1234567890"}
    {% endcapture %}

    {% assign bulkOperation = hash %}
    {% assign bulkOperation["objects"] = jsonl_string | parse_jsonl %}
  {% endif %}

  {% assign customers = bulkOperation.objects | where: "__typename", "CustomerSegmentMember" %}

  {% for customer in customers %}
    {% comment %}
      -- get additional data from this customer's last order
    {% endcomment %}

    {% capture query %}
      query {
        order(id: {{ customer.lastOrderId | json }}) {
          email
          name
          processedAt
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% if event.preview %}
      {% capture result_json %}
        {
          "data": {
            "order": {
              "email": "preview@example.com",
              "name": "%PREVIEW",
              "processedAt": {{ cutoff_date | json }}
            }
          }
        }
      {% endcapture %}

      {% assign result = result_json | parse_json %}
    {% endif %}

    {% assign order = result.data.order %}
    {% assign customer_email = customer.defaultEmailAddress.emailAddress | default: order.email %}

    {% assign processed_at_s = order.processedAt | date: "%s" | times: 1 %}
    {% assign age_d = "now" | date: "%s" | times: 1 | minus: processed_at_s | divided_by: 86400 | round %}
    {% assign age_d_modulo = age_d | modulo: interval_d %}

    {% if age_d == 0 %}
      {% continue %}

    {% elsif age_d_modulo != 0 %}
      {% capture message -%}
        Order {{ order.name }} ({{ customer_email }}) was placed on {{ processed_at_s | date: "%F" }}. Next email is due in {{ interval_d | minus: age_d_modulo }} day(s).
      {%- endcapture %}

      {% log message %}

      {% continue %}

    {% else %}
      {% assign email_options = hash %}
      {% assign email_options["to"] = customer_email %}
      {% assign email_options["subject"] = email_subject | replace: "ORDER_NUMBER", order.name %}
      {% assign email_options["body"] = email_body | replace: "ORDER_NUMBER", order.name | strip | newline_to_br %}
      {% assign email_options["reply_to"] = shop.customer_email %}
      {% assign email_options["from_display_name"] = shop.name %}

      {% if test_mode %}
        {% log email_options %}
      {% else %}
        {% action "email" email_options %}
      {% endif %}
    {% endif %}
  {% endfor %}
{% endif %}
Mechanic Liquid reference

Default option values

Email subject
It's time to reorder!
Email body
Hello,

It's been X days since your last order (ORDER_NUMBER). <a href="https://{{ shop.domain  }}/">Return to our store</a>

Thanks,
{{ shop.name }}