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

Mechanic is the one-tool-does-it-all automation app 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.

Runs when an order is paid. Configuration includes required product title, required product tags, and customer tags to apply.

15-day free trial – unlimited tasks

Documentation

For this task, you must fill in at least one of "Required product title" and "Required product tags". If you leave both blank, the task will not perform any tagging.

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)
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 event.preview %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "update",
        [
          "customer",
          12345
        ],
        {
          "tags": {{ options.customer_tags_to_apply__required_array | join: ", " | json }}
        }
      ]
    }
  }
{% elsif 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 %}
      {
        "action": {
          "type": "shopify",
          "options": [
            "update",
            [
              "customer",
              {{ order.customer.id | json }}
            ],
            {
              "tags": {{ customer_tags_to_save | json }}
            }
          ]
        }
      }
    {% endif %}
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.