Auto-tag customers within a purchase range, with Mechanic.

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

Auto-tag customers within a purchase range

Use this task to auto-tag customers when their historical purchase total falls within a certain range. For example, by configuring this task to evaluate purchase thresholds by number of orders, and by setting the minimum to "1" and the maximum to "12", this task will auto-tag customers who've made at least 1 purchase, and will remove the tag when the 13th purchase is made.

Runs Occurs whenever an order is created and Occurs whenever an order is cancelled. Configuration includes purchase minimum, purchase maximum, evaluate purchase thresholds by number of orders, evaluate purchase thresholds by adding line item quantities, evaluate purchase thresholds by adding line item subtotals, only count paid orders, only count this product id, and customer tag to apply.

15-day free trial – unlimited tasks

Documentation

Use this task to auto-tag customers when their historical purchase total falls within a certain range. For example, by configuring this task to evaluate purchase thresholds by number of orders, and by setting the minimum to "1" and the maximum to "12", this task will auto-tag customers who've made at least 1 purchase, and will remove the tag when the 13th purchase is made.

This task can total up purchases by number of orders, adding line item quantities, or by adding line item subtotals. Optionally, choose a specific product ID to filter by. Learn how to find a product ID

This task will run whenever a new order is created (or paid, if you choose to only count paid orders), and whenever an order is cancelled.

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
{% if options.only_count_paid_orders__boolean %}
  shopify/orders/paid
{% else %}
  shopify/orders/create
{% endif %}

shopify/orders/cancelled
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
purchase minimum (number, required), purchase maximum (number, required), evaluate purchase thresholds by number of orders (boolean), evaluate purchase thresholds by adding line item quantities (boolean), evaluate purchase thresholds by adding line item subtotals (boolean), only count paid orders (boolean), only count this product id (number), customer tag to apply (required)
Code
{% assign purchase_minimum = options.purchase_minimum__number_required %}
{% assign purchase_maximum = options.purchase_maximum__number_required %}

{% if purchase_minimum < 1 or purchase_maximum < 1 %}
  {% error "Purchase minimum and maximum should both be greater than zero." %}
{% elsif purchase_minimum > purchase_maximum %}
  {% error "Purchase maximum should be equal to or greater than purchase minimum." %}
{% endif %}

{% assign mode_selections = 0 %}
{% if options.evaluate_purchase_thresholds_by_number_of_orders__boolean %}
  {% assign mode = "orders" %}
  {% assign mode_selections = mode_selections | plus: 1 %}
{% endif %}
{% if options.evaluate_purchase_thresholds_by_adding_line_item_quantities__boolean %}
  {% assign mode = "quantity" %}
  {% assign mode_selections = mode_selections | plus: 1 %}
{% endif %}
{% if options.evaluate_purchase_thresholds_by_adding_line_item_subtotals__boolean %}
  {% assign mode = "subtotal" %}
  {% assign mode_selections = mode_selections | plus: 1 %}
{% endif %}

{% if mode_selections == 0 %}
  {% error "Choose one method of evaluating the purchase thresholds. :)" %}
{% elsif mode_selections > 1 %}
  {% error "Choose *only* one method of evaluating the purchase thresholds. :)" %}
{% endif %}

{% assign purchase_total = 0 %}

{% assign orders_scope = order.customer.orders.any %}
{% if options.only_count_paid_orders__boolean %}
  {% assign orders_scope = orders_scope.paid %}
{% endif %}

{% for some_order in orders_scope %}
  {% if some_order.cancelled_at != blank %}
    {% continue %}
  {% endif %}

  {% for line_item in some_order.line_items %}
    {% if options.only_count_this_product_id__number != blank and line_item.product_id != options.only_count_this_product_id__number %}
      {% continue %}
    {% endif %}

    {% case mode %}
    {% when "orders" %}
      {% assign purchase_total = purchase_total | plus: 1 %}
      {% break %}
    {% when "quantity" %}
      {% assign purchase_total = purchase_total | plus: line_item.quantity %}
    {% when "subtotal" %}
      {% assign purchase_total = line_item.price | times: line_item.quantity | plus: purchase_total %}
    {% endcase %}
  {% endfor %}
{% endfor %}

{% if event.preview %}
  {% assign purchase_total = purchase_minimum %}
  {% assign customer = '{"admin_graphql_api_id":"gid://shopify/Customer/1234567890","tags":""}' | parse_json %}
{% else %}
  {% assign customer = order.customer %}
{% endif %}

{% log message: "Purchase total for this customer", purchase_total: purchase_total  %}

{% assign customer_tags_to_match = customer.tags | downcase | split: ", " %}
{% assign customer_tag_to_match = options.customer_tag_to_apply__required | downcase %}
{% if purchase_total >= purchase_minimum and purchase_total <= purchase_maximum %}
  {% unless customer_tags_to_match contains customer_tag_to_match %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: {{ customer.admin_graphql_api_id | json }}
          tags: {{ options.customer_tag_to_apply__required | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endunless %}
{% else %}
  {% if customer_tags_to_match contains customer_tag_to_match %}
    {% action "shopify" %}
      mutation {
        tagsRemove(
          id: {{ customer.admin_graphql_api_id | json }}
          tags: {{ options.customer_tag_to_apply__required | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Evaluate purchase thresholds by number of orders
true