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.)
Try this taskWhen it runs
- Occurs whenever an order is paid
- Occurs whenever user/memberships/expire is triggered
What you choose
- Membership product title
- Membership tag
- Days to wait before untagging
- Remove tag immediately for cancelled orders
Task code and subscriptions
You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.
Subscriptions
shopify/orders/paid
{% if options.remove_tag_immediately_for_cancelled_orders__boolean %}
shopify/orders/cancelled
{% endif %}
user/memberships/expire
How task subscriptions work
Option types and flags
- Membership product title (required)
- Membership tag (required)
- Days to wait before untagging (number, required)
- Remove tag immediately for cancelled orders (boolean)
Task 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 %}
Mechanic Liquid reference
Default option values
- Membership product title
1 month membership- Membership tag
member- Days to wait before untagging
30