Auto-tag customers who purchase an item on sale, with Mechanic.

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

Auto-tag customers who purchase an item on sale

When a customer orders an item on sale (technically, when the order contains a product variant that includes a "compare at" price), this task automatically adds a tag to their account. Useful for keeping track of customers who take advantage of sale pricing!

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

15-day free trial – unlimited tasks

Documentation

When a customer orders an item on sale (technically, when the order contains a product variant that includes a "compare at" price), this task automatically adds a tag to their account. Useful for keeping track of customers who take advantage of sale pricing!

This task will run for each new order that's created, tagging customers who purchase a product that has a "compare at" price.

Run this task manually to have Mechanic scan your entire customer base, and each customer's uncancelled order history. This may take some time! To ensure that Mechanic can access your complete history, make sure "Read all orders" is enabled.

Please note: This task will only tag customers who've purchased a product that currently has a "compare at" price. Historical information about "compare at" pricing is not available, and so this task cannot make tagging decisions based on historical pricing.

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 add (required)
Code
{% assign tag_to_add = options.tag_to_add__required %}

{% if event.topic contains "shopify/orders/" %}
  {% comment %}
    -- get order and customer data
  {% endcomment %}

  {% capture query %}
    query {
      order(id: {{ order.admin_graphql_api_id | json }}) {
        id
        customer {
          id
          tags
        }
        lineItems(first: 250) {
          nodes {
            variant {
              compareAtPrice
            }
          }
        }
      }
    }
  {% 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": [
                {
                  "variant": {
                    "compareAtPrice": "100.0"
                  }
                }
              ]
            }
          }
        }
      }
    {% endcapture %}

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

  {% assign order = result.data.order %}
  {% assign customer = order.customer %}

  {% if customer == blank %}
    {% break %}
  {% endif %}

  {% comment %}
    -- if any line item has a variant with a compare at price set, then tag the customer
  {% endcomment %}

  {% for line_item in order.lineItems.nodes %}
    {% if line_item.variant.compareAtPrice != blank %}
      {% unless customer.tags contains tag_to_add %}
        {% action "shopify" %}
          mutation {
            tagsAdd(
              id: {{ customer.id | json }}
              tags: {{ tag_to_add | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          }
        {% endaction %}
      {% endunless %}

      {% break %}
    {% endif %}
  {% endfor %}

{% elsif event.topic == "mechanic/user/trigger" %}
  {% comment %}
    -- get all customers who do not yet have the tag and who have placed at least one order
  {% endcomment %}

  {% assign cursor = nil %}
  {% assign customer_ids = array %}

  {% for n in (1..100) %}
    {% capture query %}
      query {
        customerSegmentMembers(
          first: 1000
          after: {{ cursor | json }}
          query: "customer_tags NOT CONTAINS '{{ tag_to_add }}' AND number_of_orders > 0"
        ) {
          pageInfo {
            hasNextPage
            endCursor
          }
          edges {
            node {
              id
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% if event.preview %}
      {% capture result_json %}
        {
          "data": {
            "customerSegmentMembers": {
              "edges": [
                {
                  "node": {
                    "id": "gid://shopify/customerSegmentMember/1234567890"
                  }
                }
              ]
            }
          }
        }
      {% endcapture %}

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

    {% for edge in result.data.customerSegmentMembers.edges %}
      {% assign customer_ids[customer_ids.size] = edge.node.id | remove: "SegmentMember" %}
    {% endfor %}

    {% if result.data.customerSegmentMembers.pageInfo.hasNextPage %}
      {% assign cursor = result.data.customerSegmentMembers.pageInfo.endCursor %}
    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}
{% endif %}

{% for customer_id in customer_ids %}
  {% assign customer_qualifies = nil %}

  {% comment %}
    -- get uncancelled orders data for customer
  {% endcomment %}

  {% assign cursor = nil %}

  {% for n in (1..100) %}
    {% capture query %}
      query {
        customer(id: {{ customer_id | json }}) {
          orders(
            first: 50
            after: {{ cursor | json }}
            query: "-status:cancelled"
          ) {
            pageInfo {
              hasNextPage
              endCursor
            }
            nodes {
              lineItems(first: 250) {
                nodes {
                  variant {
                    compareAtPrice
                  }
                }
              }
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% if event.preview %}
      {% capture result_json %}
        {
          "data": {
            "customer": {
              "orders": {
                "nodes": [
                  {
                    "lineItems": {
                      "nodes": [
                        {
                          "variant": {
                            "compareAtPrice": "100.0"
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      {% endcapture %}

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

    {% comment %}
      -- break out of orders loop as soon as customer qualifies
    {% endcomment %}

    {% for order in result.data.customer.orders.nodes %}
      {% for line_item in order.lineItems.nodes %}
        {% if line_item.variant.compareAtPrice != blank %}
          {% assign customer_qualifies = true %}
          {% break %}
        {% endif %}
      {% endfor %}

      {% if customer_qualifies %}
        {% break %}
      {% endif %}
    {% endfor %}

    {% if result.data.customer.orders.pageInfo.hasNextPage and customer_qualifies != true %}
      {% assign cursor = result.data.customer.orders.pageInfo.endCursor %}
    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}

  {% if customer_qualifies %}
    {% unless customer.tags contains tag_to_add %}
      {% action "shopify" %}
        mutation {
          tagsAdd(
            id: {{ customer_id | json }}
            tags: {{ tag_to_add | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endunless %}
  {% endif %}
{% endfor %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Tag to add
discount-shopper