Manage tagging for a time-limited membership product, with Mechanic.

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

Manage tagging for a time-limited membership product

Use this task to automatically tag customers when they purchase specific a membership product, and then untag them after a certain amount of time has passed! (Optionally, this task can also automatically untag the customer if/when their membership order is cancelled.)

Runs Occurs whenever an order is paid and Occurs whenever user/memberships/expire is triggered. Configuration includes membership product title, membership tag, days to wait before untagging, and remove tag immediately for cancelled orders.

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
{% if options.remove_tag_immediately_for_cancelled_orders__boolean %}
  shopify/orders/cancelled
{% endif %}
user/memberships/expire
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
membership product title (required), membership tag (required), days to wait before untagging (number, required), remove tag immediately for cancelled orders (boolean)
Code
{% assign membership_product_title = options.membership_product_title__required %}
{% assign membership_tag = options.membership_tag__required %}
{% assign days_to_wait_before_untagging = options.days_to_wait_before_untagging__number_required %}

{% if order %}
  {% comment %}
    -- query the order, products, and customer for use in all events triggered by orders
  {% endcomment %}

  {% capture query %}
    query {
      order(id: {{ order.admin_graphql_api_id | json }}) {
        id
        tags
        customer {
          id
          tags
        }
        lineItems(first: 250) {
          nodes {
            product {
              title
            }
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "order": {
            "id": "gid://shopify/Order/1234567890",
            "customer": {
              "id": "gid://shopify/Customer/1234567890"
            },
            "lineItems": {
              "nodes": [
                {
                  "product": {
                    "title": {{ membership_product_title | json }}
                  }
                }
              ]
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = result_json | parse_json %}
  {% endif %}

  {% assign order = result.data.order %}
  {% assign customer = order.customer %}
  {% assign purchased_product_titles
    = order.lineItems.nodes
    | map: "product"
    | map: "title"
  %}

  {% comment %}
    -- stop processing this order if there is no customer or the membership product is not included
  {% endcomment %}

  {% unless customer != blank and purchased_product_titles contains membership_product_title %}
    {% break %}
  {% endunless %}
{% endif %}

{% if event.topic == "shopify/orders/paid" %}
  {% assign days_in_the_future_s = days_to_wait_before_untagging | times: 86400 %}
  {% assign expire_at_s = "now" | date: "%s" | plus: days_in_the_future_s %}

  {% comment %}
    -- if the customer does not already have the membership tag, then tag them and create a future event to untag them
  {% endcomment %}

  {% unless customer.tags contains membership_tag %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: {{ customer.id | json }}
          tags: {{ membership_tag | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}

    {% action "event" %}
      {
        "topic": "user/memberships/expire",
        "data": {
          "membership_tag": {{ membership_tag | json }},
          "customer_id": {{ customer.id | json }}
        },
        "run_at": {{ expire_at_s | json }},
        "task_id": {{ task.id | json }}
      }
    {% endaction %}
  {% endunless %}

{% elsif event.topic == "shopify/orders/cancelled" %}
  {% comment %}
    -- if customer still has the membership tag, then call custom event to untag them
  {% endcomment %}

  {% if customer.tags contains membership_tag or event.preview %}
    {% action "event" %}
      {
        "topic": "user/memberships/expire",
        "data": {
          "membership_tag": {{ membership_tag | json }},
          "customer_id": {{ customer.id | json }}
        },
        "task_id": {{ task.id | json }}
      }
    {% endaction %}
  {% endif %}

{% elsif event.topic == "user/memberships/expire" %}
  {% comment %}
    -- get the customer ID and membership tag from the event data, as the tag may have changed in the task config since the customer was originally tagged
  {% endcomment %}

  {% assign customer_id = event.data.customer_id %}
  {% assign membership_tag = event.data.membership_tag %}

  {% capture query %}
    query {
      customer(id: {{ customer_id | json }}) {
        id
        tags
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "customer": {
            "id": "gid://shopify/Customer/1234567890",
            "tags": {{ membership_tag | json }}
          }
        }
      }
    {% endcapture %}

    {% assign result = result_json | parse_json %}
  {% endif %}

  {% assign customer = result.data.customer %}

  {% if customer.tags contains membership_tag %}
    {% action "shopify" %}
      mutation {
        tagsRemove(
          id: {{ customer.id | json }}
          tags: {{ membership_tag | 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
Membership product title
1 month membership
Membership tag
member
Days to wait before untagging
30