Auto-tag customers when they purchase a matching product, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Auto-tag customers when they purchase a matching product

by Isaac Bowen (team@usemechanic.com)

Configure this task with a product title and/or some tags to watch for, and this task will apply the tag(s) of your choice to every customer who makes a matching purchase. Run this task manually to tag all customers who have a paid order already on file for a qualifying product.

Runs when an order is paid, when a user triggers the task, and when a bulk operation finishes. Configuration includes required product title, required product tags, and customer tags to apply.

15-day free trial – unlimited tasks

Documentation

This task runs whenever an order is paid, tagging the customer if they've purchased a qualifying product. Use the "Run task" button to scan all existing orders, tagging customers who have a paid order already on file for a qualifying product.

Note: If you fill in "Required product tags", this task will look for a product that has all of the required tags by itself.

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 a user triggers the task (mechanic/user/trigger)
when a bulk operation finishes (mechanic/shopify/bulk_operation)
Options
required product title, required product tags (array), customer tags to apply (required, array)
Script
{% comment %}
  Ordering the task options:

  {{ options.required_product_title }}
  {{ options.required_product_tags__array }}
  {{ options.customer_tags_to_apply__required_array }}
{% endcomment %}

{% if options.required_product_title == blank and options.required_product_tags__array == blank %}
  {% error "Fill in at least one of 'Required product title' and 'Required product tags'. :)" %}
{% endif %}

{% if event.topic contains "shopify/orders/" %}
  {% if event.preview %}
    {% assign order = hash %}
    {% assign order["financial_status"] = "paid" %}
    {% assign order["customer"] = hash %}
    {% assign order["customer"]["admin_graphql_api_id"] = "gid://shopify/Order/1234567890" %}
    {% assign order["line_items"] = array %}

    {% assign product = hash %}
    {% if options.required_product_title != blank %}
      {% assign product["title"] = options.required_product_title %}
    {% endif %}
    {% if options.required_product_tags__array  != blank %}
      {% assign product["tags"] = options.required_product_tags__array | join: ", " %}
    {% endif %}

    {% assign order["line_items"][0] = hash %}
    {% assign order["line_items"][0]["product"] = product %}
  {% endif %}

  {% if order.financial_status == "paid" and order.customer %}
    {% assign order_qualifies = false %}
    {% assign order_qualifies_by_tag = nil %}
    {% assign order_qualifies_by_title = nil %}

    {% assign order_products = order.line_items | map: "product" %}

    {% if options.required_product_title != blank %}
      {% assign order_qualifies_by_title = false %}

      {% assign order_product_titles = order_products | map: "title" %}
      {% if order_product_titles contains options.required_product_title %}
        {% assign order_qualifies_by_title = true %}
      {% endif %}
    {% endif %}

    {% if options.required_product_tags__array != blank %}
      {% assign order_qualifies_by_tag = false %}

      {% for order_product in order_products %}
        {% assign order_product_tags = order_product.tags | split: ", " %}
        {% assign order_product_qualifies_by_tag = true %}
        {% for required_tag in options.required_product_tags__array %}
          {% unless order_product_tags contains required_tag %}
            {% assign order_product_qualifies_by_tag = false %}
            {% break %}
          {% endunless %}
        {% endfor %}

        {% if order_product_qualifies_by_tag %}
          {% assign order_qualifies_by_tag = true %}
          {% break %}
        {% endif %}
      {% endfor %}
    {% endif %}

    {% comment %}
      Make sure *at least one* of the two qualifications were evaluated,
      and make sure neither of them outright failed.
    {% endcomment %}
    {% if order_qualifies_by_tag != nil or order_qualifies_by_title != nil %}
      {% if order_qualifies_by_tag != false and order_qualifies_by_title != false %}
        {% assign order_qualifies = true %}
      {% endif %}
    {% endif %}

    {% if order_qualifies %}
      {% assign customer_tags_to_save = order.customer.tags | add_tags: options.customer_tags_to_apply__required_array %}
      {% if order.customer.tags == customer_tags_to_save %}
        {"log": "This order qualifies, but the customer already has all tags that would be applied."}
      {% else %}
        {% action "shopify" %}
          mutation {
            tagsAdd(
              id: {{ order.customer.admin_graphql_api_id | json }}
              tags: {{ options.customer_tags_to_apply__required_array | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          }
        {% endaction %}
      {% endif %}
    {% endif %}
  {% endif %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% comment %}
    If you encounter timeouts, adjust `customers_query` and/or `orders_query` to limit
    the amount of data that Shopify has to churn through.
  {% endcomment %}

  {% assign customers_query = nil %}
  {% assign orders_query = "financial_status:paid" %}

  {% capture bulk_operation_query %}
    query {
      customers(query: {{ customers_query | json }}) {
        edges {
          node {
            __typename
            id
            tags
            orders(query: {{ orders_query | json }}) {
              edges {
                node {
                  __typename
                  id
                  lineItems {
                    edges {
                      node {
                        __typename
                        id
                        product {
                          __typename
                          id
                          title
                          tags
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  {% endcapture %}

  {% action "shopify" %}
    mutation {
      bulkOperationRunQuery(
        query: {{ bulk_operation_query | json }}
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
  {% assign customer_ids_to_tag = array %}

  {% assign lineItems = bulkOperation.objects | where: "__typename", "LineItem" | where: "product" %}
  {% for lineItem in lineItems %}
    {% assign product = lineItem.product %}
    {% assign order = lineItem.__parent %}
    {% assign customer = order.__parent %}

    {% if customer_ids_to_tag contains customer.id %}
      {% continue %}
    {% endif %}

    {% assign product_qualifies = false %}
    {% assign product_qualifies_by_tag = nil %}
    {% assign product_qualifies_by_title = nil %}

    {% if options.required_product_title != blank %}
      {% assign product_qualifies_by_title = false %}

      {% if product.title == options.required_product_title %}
        {% assign product_qualifies_by_title = true %}
      {% endif %}
    {% endif %}

    {% if options.required_product_tags__array != blank %}
      {% assign product_qualifies_by_tag = true %}
      {% for required_tag in options.required_product_tags__array %}
        {% unless product.tags contains required_tag %}
          {% assign product_qualifies_by_tag = false %}
          {% break %}
        {% endunless %}
      {% endfor %}
    {% endif %}

    {% comment %}
      Make sure *at least one* of the two qualifications were evaluated,
      and make sure neither of them outright failed.
    {% endcomment %}
    {% if product_qualifies_by_tag != nil or product_qualifies_by_title != nil %}
      {% if product_qualifies_by_tag != false and product_qualifies_by_title != false %}
        {% assign product_qualifies = true %}
      {% endif %}
    {% endif %}

    {% if product_qualifies %}
      {% assign customer_ids_to_tag[customer_ids_to_tag.size] = customer.id %}
    {% endif %}
  {% endfor %}

  {% if event.preview %}
    {% assign customer_ids_to_tag[0]=  "gid://shopify/Customer/1234567890" %}
    {% assign bulkOperation = hash %}
    {% assign bulkOperation["objects"] = array %}
    {% assign bulkOperation["objects"][0] = '{"id":"gid://shopify/Customer/1234567890","tags":""}' | parse_json %}
  {% endif %}

  {% for customer_id in customer_ids_to_tag %}
    {% assign customer = bulkOperation.objects | where: "id", customer_id | first %}

    {% assign tags_to_add = array %}
    {% for tag in options.customer_tags_to_apply__required_array %}
      {% unless customer.tags contains tag %}
        {% assign tags_to_add[tags_to_add.size] = tag %}
      {% endunless %}
    {% endfor %}

    {% if tags_to_add == empty %}
      {% capture log_message %}Customer {{ customer.id }} already has all applicable tags ({{ tags_applicable | join: ", " }}); nothing to do.{% endcapture %}
      {"log": {{ log_message | json }}}
    {% else %}
      {% action "shopify" %}
        mutation {
          tagsAdd(
            id: {{ customer.id | json }}
            tags: {{ tags_to_add | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.