Mechanic Get Mechanic
Task library

Auto-tag a customer's first order

Watches for incoming orders that are the very first order on file for the related customer, and adds the tag of your choice.

Try this task

When it runs

Occurs whenever an order is created

What you choose

Order tag to add

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/create
How task subscriptions work

Option types and flags

Task code

{% unless order.customer or event.preview %}
  {% log "This order does not have a customer." %}
  {% break %}
{% endunless %}

{% capture query %}
  query {
    customer(id: {{ order.customer.admin_graphql_api_id | json }}) {
      numberOfOrders
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "customer": {
          "numberOfOrders": "1"
        }
      }
    }
  {% endcapture %}

  {% assign result = result_json | parse_json %}
{% endif %}

{% if result.data.customer.numberOfOrders == "1" %}
  {% action "shopify" %}
    mutation {
      tagsAdd(
        id: {{ order.admin_graphql_api_id | json }}
        tags: {{ options.order_tag_to_add__required | json }}
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% else %}
  {% log numberOfOrders: result.data.customer.numberOfOrders %}
{% endif %}
Mechanic Liquid reference