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

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

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

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 Occurs whenever 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

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

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, 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
shopify/orders/create
Tasks use subscriptions to sign up for specific kinds of events. Learn more
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)
Code
{% 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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
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 }}