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, when an order is updated, and 1 month after user/memberships/expire is triggered. Configuration includes membership product title, membership tag, and remove tag for cancelled orders.

15-day free trial – unlimited tasks

Documentation

By default, this task will expire the customer's membership after 1 month. To change this, click "Advanced Settings", then change set the final "Subscriptions" line based on these examples:

  • For 15 days, use user/memberships/expire+15.days
  • For 1 month, use user/memberships/expire+1.month
  • For 6 months, use user/memberships/expire+6.months
  • For 1 year, use user/memberships/expire+1.year

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 an order is updated (shopify/orders/updated)
1 month after user/memberships/expire is triggered (user/memberships/expire+1.month)
Options
membership product title (required), membership tag (required), remove tag for cancelled orders (boolean)
Script
{% 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" %}
  {% if event.preview %}
    {
      "action": {
        "type": "shopify",
        "options": [
          "update",
          [
            "customer",
            1234567890
          ],
          {
            "tags": {{ options.membership_tag__required | json }}
          }
        ]
      }
    }

    {
      "action": {
        "type": "event",
        "options": {
          "topic": "user/memberships/expire",
          "data": {
            "membership_tag": {{ options.membership_tag__required | json }},
            "customer_id": 1234567890
          }
        }
      }
    }
  {% elsif order_includes_membership_product %}
    {% assign customer = order.customer.reload %}
    {% assign customer_tags_to_save = customer.tags | add_tag: options.membership_tag__required %}
    {% if customer.tags != customer_tags_to_save %}
      {
        "action": {
          "type": "shopify",
          "options": [
            "update",
            [
              "customer",
              {{ customer.id | json }}
            ],
            {
              "tags": {{ customer_tags_to_save | json }}
            }
          ]
        }
      }

      {
        "action": {
          "type": "event",
          "options": {
            "topic": "user/memberships/expire",
            "data": {
              "membership_tag": {{ options.membership_tag__required | json }},
              "customer_id": {{ customer.id | json }}
            }
          }
        }
      }
    {% endif %}
  {% endif %}
{% elsif event.topic == "shopify/orders/updated" %}
  {% if event.preview %}
    {
      "action": {
        "type": "shopify",
        "options": [
          "update",
          [
            "customer",
            1234567890
          ],
          {
            "tags": ""
          }
        ]
      }
    }
  {% elsif order_includes_membership_product and options.remove_tag_for_cancelled_orders__boolean %}
    {% assign customer_tags = order.customer.tags | split: ", " %}
    {% if order.cancelled_at != blank and customer_tags contains options.membership_tag__required %}
      {% assign customer = order.customer.reload %}
      {% assign customer_tags_to_save = customer.tags | remove_tag: options.membership_tag__required %}
      {% if customer.tags != customer_tags_to_save %}
        {
          "action": {
            "type": "shopify",
            "options": [
              "update",
              [
                "customer",
                {{ customer.id | json }}
              ],
              {
                "tags": {{ customer_tags_to_save | json }}
              }
            ]
          }
        }
      {% endif %}
    {% endif %}
  {% endif %}
{% elsif event.topic == "user/memberships/expire" %}
  {% if event.preview %}
    {
      "action": {
        "type": "shopify",
        "options": [
          "update",
          [
            "customer",
            1234567890
          ],
          {
            "tags": ""
          }
        ]
      }
    }
  {% elsif event.data.membership_tag == options.membership_tag__required %}
    {% assign customer = shop.customers[event.data.customer_id] %}
    {% assign customer_tags_to_save = customer.tags | remove_tag: options.membership_tag__required %}
    {% if customer.tags != customer_tags_to_save %}
      {
        "action": {
          "type": "shopify",
          "options": [
            "update",
            [
              "customer",
              {{ customer.id | json }}
            ],
            {
              "tags": {{ customer_tags_to_save | json }}
            }
          ]
        }
      }
    {% 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
Remove tag for cancelled orders
true