Mechanic Get Mechanic
Task library

Manage annual memberships based on order minimums

Use this task to automatically tag customers as members when they purchase a minimum quantity of products. And, check on those customers on a regular basis to ensure that they maintain their order quantity minimums.

Try this task

When it runs

  • Occurs whenever an order is paid
  • Occurs whenever user/membership/renewal is triggered, with a 12 month delay

What you choose

  • Minimum qualifying order quantity
  • Membership tag
  • Months of order history to consider for renewal
  • Minimum qualifying order quantity for renewal
  • Months between renewal checks
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

shopify/orders/paid
user/membership/renewal+{{ options.months_between_renewal_checks__number_required | default: 12 }}.months
How task subscriptions work

Option types and flags

Task code

{% if event.topic == "shopify/orders/paid" %}
  {% assign order_qualifies = false %}

  {% if order.customer %}
    {% assign order_quantity_total = order.line_items | map: "quantity" | sum %}

    {% if order_quantity_total >= options.minimum_qualifying_order_quantity__number_required %}
      {% assign order_qualifies = true %}
    {% endif %}
  {% endif %}

  {% if event.preview or order_qualifies %}
    {% assign customer_is_member = false %}
    {% assign customer_tags = customer.tags | split: ", " %}
    {% if customer_tags contains options.membership_tag__required %}
      {% assign customer_is_member = true %}
      {"log": "Customer is already a member"}
    {% endif %}

    {% if event.preview or customer_is_member == false %}
      {
        "action": {
          "type": "shopify",
          "options": [
            "update",
            [
              "customer",
              {{ order.customer.id | default: 1234567890 | json }}
            ],
            {
              "tags": {{ order.customer.tags | add_tag: options.membership_tag__required | json }}
            }
          ]
        }
      }

      {
        "action": {
          "type": "event",
          "options": {
            "topic": "user/membership/renewal",
            "data": {
              "customer_id": {{ order.customer.id | default: 1234567890 | json }}
            }
          }
        }
      }
    {% endif %}
  {% endif %}
{% elsif event.topic == "user/membership/renewal" %}
  {% assign customer = shop.customers[event.data.customer_id] %}
  {% assign customer_tags = customer.tags | split: ", " %}

  {% if event.preview or customer_tags contains options.membership_tag__required %}
    {% assign customer_order_quantity_total = 0 %}

    {% assign seconds_in_a_month = 30 | times: 24 | times: 60 | times: 60 %}
    {% assign seconds_of_order_history_to_consider = options.months_of_order_history_to_consider_for_renewal__number_required | times: seconds_in_a_month %}
    {% assign order_date_minimum_s = "now" | date: "%s" | minus: seconds_of_order_history_to_consider %}

    {% for order in customer.orders.any.paid %}
      {% assign order_date_s = order.created_at | date: "%s" | times: 1 %}
      {% if order_date_s < order_date_minimum_s %}
        {% break %}
      {% endif %}

      {% assign customer_order_quantity_total = order.line_items | map: "quantity" | sum | plus: customer_order_quantity_total %}
    {% endfor %}

    {% if event.preview or customer_order_quantity_total < options.minimum_qualifying_order_quantity_for_renewal__number_required %}
      {
        "action": {
          "type": "shopify",
          "options": [
            "update",
            [
              "customer",
              {{ order.customer.id | default: 1234567890 | json }}
            ],
            {
              "tags": {{ customer.tags | remove_tag: options.membership_tag__required | json }}
            }
          ]
        }
      }
    {% else %}
      {
        "action": {
          "type": "event",
          "options": {
            "topic": "user/membership/renewal",
            "data": {
              "customer_id": {{ order.customer.id | json }}
            }
          }
        }
      }
    {% endif %}
  {% else %}
    {"log": "Customer does not have the membership tag; skipping renewal check"}
  {% endif %}
{% endif %}
Mechanic Liquid reference

Default option values

Months of order history to consider for renewal
12
Months between renewal checks
12