Manage annual memberships based on order minimums with Mechanic.

Mechanic is an automation and development platform for Shopify. :)

Manage annual memberships based on order minimums

by Isaac Bowen (team@usemechanic.com)

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.

Runs when an order is paid and 12 months after user/membership/renewal is triggered. Configuration includes minimum qualifying order quantity, membership tag, months of order history to consider for renewal, minimum qualifying order quantity for renewal, and months between renewal checks.

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 paid (shopify/orders/paid)
12 months after user/membership/renewal is triggered (user/membership/renewal+12.months)
Options
minimum qualifying order quantity (number, required), membership tag (required), months of order history to consider for renewal (number, required), minimum qualifying order quantity for renewal (number, required), months between renewal checks (number, required)
Script
{% 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 tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Months of order history to consider for renewal
12
Months between renewal checks
12