Mechanic Get Mechanic
Task library

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.

Try this task

When it runs

  • Occurs whenever an order is created
  • Occurs whenever an order is cancelled

What you choose

  • Purchase minimum
  • Purchase maximum
  • Evaluate purchase thresholds by number of orders
Show 5 more
  • 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
  • Customer tag to apply

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.

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

{% if options.only_count_paid_orders__boolean %}
  shopify/orders/paid
{% else %}
  shopify/orders/create
{% endif %}

shopify/orders/cancelled
How task subscriptions work

Option types and flags

Task 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 %}
Mechanic Liquid reference

Default option values

Evaluate purchase thresholds by number of orders
true
Evaluate purchase thresholds by adding line item quantities
false
Evaluate purchase thresholds by adding line item subtotals
false
Only count paid orders
false