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

Mechanic is an automation development platform for Shopify. :)

Manage tagging for a time-limited membership product

by Isaac Bowen (team@usemechanic.com)

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 when an order is paid and when 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, 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)
when user/memberships/expire is triggered (user/memberships/expire)
Options
membership product title (required), membership tag (required), days to wait before untagging (number, required), remove tag immediately for cancelled orders (boolean)
Script
{% comment %}
  A preference on option order:

  {{ options.membership_product_title__required }}
  {{ options.membership_tag__required }}
  {{ options.days_to_wait_before_untagging__number_required }}
  {{ options.remove_tag_immediately_for_cancelled_orders__boolean }}
{% endcomment %}

{% assign order_includes_membership_product = false %}
{% if order %}
  {% assign purchased_product_titles = order.line_items | map: "product" | map: "title" | sort | uniq %}

  {% if options.membership_product_title__required != blank and purchased_product_titles contains options.membership_product_title__required %}
    {% assign order_includes_membership_product = true %}
  {% endif %}
{% endif %}

{% if event.topic == "shopify/orders/paid" %}
  {% assign days_in_the_future_s = options.days_to_wait_before_untagging__number_required | times: 24 | times: 60 | times: 60 %}
  {% assign expire_at_s = "now" | date: "%s" | plus: days_in_the_future_s %}

  {% if event.preview %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: "gid://shopify/Customer/1234567890"
          tags: {{ options.membership_tag__required | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}

    {% action "event" %}
      {
        "topic": "user/memberships/expire",
        "data": {
          "membership_tag": {{ options.membership_tag__required | json }},
          "customer_id": 1234567890
        },
        "run_at": {{ expire_at_s | json }},
        "task_id": {{ task.id | json }}
      }
    {% endaction %}
  {% elsif order_includes_membership_product %}
    {% assign customer = order.customer.reload %}
    {% assign customer_tags = customer.tags | split: ", " %}
    {% unless customer_tags contains options.membership_tag__required %}
      {% action "shopify" %}
        mutation {
          tagsAdd(
            id: {{ customer.admin_graphql_api_id | json }}
            tags: {{ options.membership_tag__required | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}

      {% action "event" %}
        {
          "topic": "user/memberships/expire",
          "data": {
            "membership_tag": {{ options.membership_tag__required | json }},
            "customer_id": {{ customer.id | json }}
          },
          "run_at": {{ expire_at_s | json }},
          "task_id": {{ task.id | json }}
        }
      {% endaction %}
    {% endunless %}
  {% endif %}
{% elsif event.topic == "shopify/orders/updated" and options.remove_tag_immediately_for_cancelled_orders__boolean %}
  {% if event.preview %}
    {% action "event" %}
      {
        "topic": "user/memberships/expire",
        "data": {
          "membership_tag": {{ options.membership_tag__required | json }},
          "customer_id": {{ order.customer.id | json }}
        },
        "task_id": {{ task.id | json }}
      }
    {% endaction %}
  {% elsif order_includes_membership_product %}
    {% assign customer = order.customer.reload %}
    {% assign customer_tags = customer.tags | split: ", " %}
    {% if order.cancelled_at != blank and customer_tags contains options.membership_tag__required %}
      {% action "event" %}
        {
          "topic": "user/memberships/expire",
          "data": {
            "membership_tag": {{ options.membership_tag__required | json }},
            "customer_id": {{ customer.id | json }}
          },
          "task_id": {{ task.id | json }}
        }
      {% endaction %}
    {% endif %}
  {% endif %}
{% elsif event.topic == "user/memberships/expire" %}
  {% if event.preview %}
    {% action "shopify" %}
      mutation {
        tagsRemove(
          id: "gid://shopify/Customer/1234567890" 
          tags: {{ options.membership_tag__required | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% elsif event.data.membership_tag == options.membership_tag__required %}
    {% assign customer = shop.customers[event.data.customer_id] %}
    {% assign customer_tags = customer.tags | split: ", " %}
    {% if customer_tags contains options.membership_tag__required %}
      {% action "shopify" %}
        mutation {
          tagsRemove(
            id: {{ customer.admin_graphql_api_id | json }}
            tags: {{ options.membership_tag__required | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Membership product title
1 month membership
Membership tag
member
Days to wait before untagging
30