Auto-add an order note for returning customers, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Auto-add an order note for returning customers

by Isaac Bowen (team@usemechanic.com)

Use this task to identify orders that are for customers who have previously ordered from your store. Optionally, ignore more frequent purchasers, by ignoring customers who've made an order recently. Useful for making sure to include a welcome-back gift in order shipments. :)

Runs when an order is created. Configuration includes ignore customers who previously ordered after this date, ignore customers who previously ordered less than x days ago, and order note to add.

15-day free trial – unlimited tasks

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
ignore customers who previously ordered after this date, ignore customers who previously ordered less than x days ago (number), order note to add (required, multiline)
Script
{% capture newline %}
{% endcapture %}

{% if options.ignore_customers_who_previously_ordered_after_this_date != blank and options.ignore_customers_who_previously_ordered_less_than_x_days_ago__number != blank %}
  {"error": "Choose only one \"ignore\" option at a time. Thanks! :)"}
{% endif %}

{% if options.ignore_customers_who_previously_ordered_after_this_date != blank %}
  {% assign nonsense_s = "asdf" | date: "%s" | times: 1 %}
  {% assign previous_order_threshold_s = options.ignore_customers_who_previously_ordered_after_this_date | date: "%s" | times: 1 %}
  {% if previous_order_threshold_s == nonsense_s %}
    {"error": {{ options.ignore_customers_who_previously_ordered_after_this_date | json | append: " was not recognized as a valid date" | json }}}
  {% endif %}
{% elsif options.ignore_customers_who_previously_ordered_less_than_x_days_ago__number != blank %}
  {% assign time_window_s = options.ignore_customers_who_previously_ordered_less_than_x_days_ago__number | times: 24 | times: 60 | times: 60 %}
  {% assign previous_order_threshold_s = "now" | date: "%s" | minus: time_window_s %}
{% endif %}

{% if previous_order_threshold_s and event.preview %}
  {% action "echo" %}
    ["Ignoring when previous order was after:", {{ previous_order_threshold_s | date: "%Y-%m-%d %T %Z" | json }}]
  {% endaction %}
{% endif %}

{% assign previous_order_qualifies = false %}
{% if previous_order_threshold_s == nil %}
  {% assign previous_order_qualifies = true %}
{% else %}
  {% assign previous_order = order.customer.orders.first %}
  {% if previous_order %}
    {% assign previous_order_created_at_s = order.customer.orders.first.created_at | date: "%s" | times: 1 %}
    {% if previous_order_created_at_s < previous_order_threshold_s %}
      {% assign previous_order_qualifies = true %}
    {% endif %}
  {% endif %}
{% endif %}

{% if event.preview or previous_order_created_at_s < previous_order_threshold_s %}
  {% assign note = order.note | append: newline | append: newline | append: options.order_note_to_add__required_multiline | strip %}

  {% action "shopify" %}
    [
      "update",
      [
        "order",
        {{ order.id | json }}
      ],
      {
        "note": {{ note | 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
Ignore customers who previously ordered less than x days ago
60
Order note to add
Add the prodigal customer insert