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/updated
{% 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
{% 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 %}
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