Auto-tag orders that contain subscription products, with Mechanic.

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

Auto-tag orders that contain subscription products

Use this task to tag incoming orders that contain subscription products. This task can also be run manually to scan and tag historical orders.

Runs Occurs whenever an order is created and Occurs when a user manually triggers the task. Configuration includes tag to apply.

15-day free trial – unlimited tasks

Documentation

Use this task to tag incoming orders that contain subscription products. This task can also be run manually to scan and tag historical orders.

This tasks looks for products that have a selling plan, e.g. "Subscription, Delivery every month". You can learn more about Selling plans here.

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/create
mechanic/user/trigger
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
tag to apply (required)
Code
{% assign orders = array %}

{% if event.topic == "shopify/orders/create" %}
  {% capture query %}
    query {
      order(id: {{ order.admin_graphql_api_id | json }}) {
        id
        name
        tags
        lineItems(first: 100) {
          edges {
            node {
              id
              sellingPlan {
                name
              }
            }
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% assign orders[0] = result.data.order %}

{% elsif event.topic == "mechanic/user/trigger" %}
  {% assign cursor = nil %}

  {% for n in (1..2500) %}
    {% capture query %}
      query {
        orders(
          first: 4
          after: {{ cursor | json }}
          reverse: true
        ) {
          pageInfo {
            hasNextPage
          }
          edges {
            cursor
            node {
              id
              name
              tags
              lineItems(first: 100) {
                edges {
                  node {
                    id
                    sellingPlan {
                      name
                    }
                  }
                }
              }
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% assign orders_batch = result.data.orders.edges | map: "node" %}
    {% assign orders = orders | concat: orders_batch %}

    {% if result.data.orders.pageInfo.hasNextPage %}
      {% assign cursor = result.data.orders.edges.last.cursor %}
    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}
{% endif %}

{% if event.preview %}
  {% capture orders_json %}
    [
      {
        "id": "gid://shopify/Order/1234567890",
        "name": "#TEST",
        "lineItems": {
          "edges": [
            {
              "node": {
                "sellingPlan": {
                  "name": "Subscription, Delivery every month"
                }
              }
            }
          ]
        }
      }
    ]
  {% endcapture %}

  {% assign orders = orders_json | parse_json %}
{% endif %}

{% for order in orders %}
  {% assign order_qualifies = false %}

  {% assign line_items = order.lineItems.edges | map: "node" %}

  {% for line_item in line_items %}
    {% if line_item.sellingPlan.name != blank %}
      {% assign order_qualifies = true %}
      {% log line_item.sellingPlan.name %}
      {% break %}
    {% endif %}
  {% endfor %}

  {% if order_qualifies %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: {{ order.id | json }}
          tags: {{ options.tag_to_apply__required | json }}
        ) {
          node {
            ... on Order {
              id
              name
              tags
            }
          }
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endfor %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Tag to apply
subscription order