Send an email alert when a customer places more than 2 orders in 24 hours, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Send an email alert when a customer places more than 2 orders in 24 hours

by Isaac Bowen (team@usemechanic.com)

Use this task to receive an email alert whenever a single customer exceeds a configurable order count, over the last configurable number of hours.

Runs when an order is created. Configuration includes order count threshold, number of hours to look back, email recipient, email subject, and email body.

15-day free trial – unlimited tasks

Documentation

Note: This task does not distinguish by order source. When counting orders, all order sources are considered (e.g. the online storefront, the Shopify admin, other apps, etc).

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
when an order is created (shopify/orders/create)
Options
order count threshold (number, required), number of hours to look back (number, required), email recipient (email, required), email subject (required), email body (required, multiline)
Script
{% if options.order_count_threshold__number_required > 250 or options.order_count_threshold__number_required <= 0 %}
  {"error": "The 'Order count threshold' option must be between 1 and 250."}
{% endif %}

{% assign now_s = "now" | date: "%s" | times: 1 %}
{% assign hour_s = 60 | times: 60 %}
{% assign threshold_interval_s = hour_s | times: options.number_of_hours_to_look_back__number_required %}
{% assign threshold_s = now_s | minus: threshold_interval_s %}

{% capture query %}
  query {
    customer(
      id: {{ order.customer.admin_graphql_api_id | json }}
    ) {
      orders(
        first: {{ options.order_count_threshold__number_required | json }}
        sortKey: CREATED_AT
        reverse: true
      ) {
        edges {
          node {
            id
            createdAt
            name
          }
        }
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% assign order_count = 0 %}

{% assign orderNodes = result.data.customer.orders.edges | map: "node" %}
{% for orderNode in orderNodes %}
  {% assign orderNode_createdAt_s = orderNode.createdAt | date: "%s" | times: 1 %}
  {% if orderNode_createdAt_s >= threshold_s %}
    {% assign order_count = order_count | plus: 1 %}
  {% endif %}
{% endfor %}

{"log": {"order_count": {{ order_count | json }}}}

{% if event.preview or order_count >= options.order_count_threshold__number_required %}
  {% action "email" %}
    {
      "to": {{ options.email_recipient__email_required | 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 %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Order count threshold
2
Number of hours to look back
24
Email subject
{{ order.customer.email }} has placed >= 2 orders in the last 24 hours
Email body
Hello,

The customer {{ order.customer.email }} has exceeded the configured order threshold.

<a href="https://{{ shop.domain }}/admin/customers/{{ order.customer.id }}">Manage this customer in Shopify</a>

Thanks,
Mechanic, for {{ shop.name }}