Manage annual memberships based on order minimums, with Mechanic.

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

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.

Runs Occurs whenever an order is paid and Occurs whenever user/membership/renewal is triggered, with a 12 month delay. 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, 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
shopify/orders/paid
user/membership/renewal+{{ options.months_between_renewal_checks__number_required | default: 12 }}.months
Tasks use subscriptions to sign up for specific kinds of events. Learn more
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)
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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Months of order history to consider for renewal
12
Months between renewal checks
12