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

Mechanic is a development platform for Shopify. :)

Auto-add an order note for returning customers

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.

Open source
View on GitHub to contribute to this task
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
{% 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 %}

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

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "customer": {
          "orders": {
            "edges": [
              {
                "node": {
                  "id": "gid://shopify/Order/2345678901",
                  "createdAt": "2020-00-00T00:00:00Z",
                  "name": "#1235"
                }
              },
              {
                "node": {
                  "id": "gid://shopify/Order/1234567890",
                  "createdAt": "1900-00-00T00:00:00Z",
                  "name": "#1234"
                }
              }
            ]
          }
        }
      }
    }
  {% endcapture %}

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

{% assign previous_order_node = result.data.customer.orders.edges[1].node %}

{% log previous_order: previous_order_node %}

{% 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 }}{% enderror %}
  {% 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 %}
  {% log %}{"previous_order_threshold": {{ previous_order_threshold_s | date: "%Y-%m-%d %T %Z" | json }}}{% endlog %}
{% endif %}

{% assign previous_order_qualifies = false %}
{% if previous_order_threshold_s == nil %}
  {% assign previous_order_qualifies = true %}
{% else %}
  {% if previous_order_node %}
    {% assign previous_order_created_at_s = previous_order_node.createdAt | date: "%s" | times: 1 %}
    {% if previous_order_created_at_s < previous_order_threshold_s %}
      {% assign previous_order_qualifies = true %}
    {% endif %}
  {% endif %}
{% endif %}

{% if 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" %}
    mutation {
      orderUpdate(
        input: {
          id: {{ order.admin_graphql_api_id | json }}
          note: {{ note | json }}
        }
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% 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