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

Mechanic is the one-tool-does-it-all automation app for Shopify. :)

Auto-tag customers who purchase an item on sale

by Isaac Bowen (team@usemechanic.com)

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 when an order is created and when a user triggers the task. Configuration includes tag to add.

15-day free trial – unlimited tasks

Documentation

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 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, 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 created (shopify/orders/create)
when a user triggers the task (mechanic/user/trigger)
Options
tag to add (required)
Script
{% if event.preview %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "update",
        [
          "customer",
          1234567890
        ],
        {
          "tags": {{ options.tag_to_add__required | json }}
        }
      ]
    }
  }
{% elsif event.topic contains "shopify/orders/" %}
  {% assign customer = order.customer.reload %}

  {% assign purchase_includes_compare_at_pricing = false %}

  {% for line_item in order.line_items %}
    {% if line_item.variant.compare_at_price != blank %}
      {% assign purchase_includes_compare_at_pricing = true %}
      {% break %}
    {% endif %}
  {% endfor %}

  {% if purchase_includes_compare_at_pricing %}
    {% assign customer_tags_to_save = customer.tags | add_tag: options.tag_to_add__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 %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% for customer in shop.customers %}
    {% assign customer_tags_to_save = customer.tags %}
    {% assign purchase_includes_compare_at_pricing = false %}

    {% for order in customer.orders.any %}
      {% for line_item in order.line_items %}
        {% if line_item.variant.compare_at_price != blank %}
          {% assign purchase_includes_compare_at_pricing = true %}
          {% break %}
        {% endif %}
      {% endfor %}

      {% if purchase_includes_compare_at_pricing %}
        {% assign customer_tags_to_save = customer.tags | add_tag: options.tag_to_add__required %}
        {% break %}
      {% endif %}
    {% endfor %}

    {% if customer_tags_to_save != customer.tags %}
      {
        "action": {
          "type": "shopify",
          "options": [
            "update",
            [
              "customer",
              {{ customer.id | json }}
            ],
            {
              "tags": {{ customer_tags_to_save | json }}
            }
          ]
        }
      }
    {% endif %}
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Tag to add
discount-shopper