Auto-tag customers with the location of their purchase with Mechanic.

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

Auto-tag customers with the location of their purchase

by Isaac Bowen (team@usemechanic.com)

When an order is created, this task adds the location of the purchase to the customer's tags. Useful for stores with multiple Shopify-powered locations.

Runs when an order is created and when a user triggers the task. Configuration includes tag for online orders.

15-day free trial – unlimited tasks

Documentation

This task will run for each new order that's created, applying the order location as a customer tag. Optionally, define a tag to be used for online orders (i.e. orders that have no location).

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.

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 for online orders
Script
{% if event.preview %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "update",
        [
          "customer",
          1234567890
        ],
        {
          "tags": "Ontario HQ"
        }
      ]
    }
  }
{% elsif event.topic contains "shopify/orders/" %}
  {% assign customer = order.customer.reload %}

  {% if order.location_id == blank and options.tag_for_online_orders != blank %}
    {% assign order_location = options.tag_for_online_orders %}
  {% elsif order.location %}
    {% assign order_location = order.location.name %}
  {% endif %}

  {% assign customer_tags_to_save = customer.tags | add_tag: order_location %}

  {% if customer_tags_to_save != customer.tags %}
    {
      "action": {
        "type": "shopify",
        "options": [
          "update",
          [
            "customer",
            {{ customer.id | json }}
          ],
          {
            "tags": {{ customer_tags_to_save | json }}
          }
        ]
      }
    }
  {% endif %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% for customer in shop.customers %}
    {% assign customer_tags_to_save = customer.tags %}

    {% for order in customer.orders.any %}
      {% if order.location_id == blank and options.tag_for_online_orders != blank %}
        {% assign order_location = options.tag_for_online_orders %}
      {% elsif order.location %}
        {% assign order_location = order.location.name %}
      {% endif %}

      {% assign customer_tags_to_save = customer_tags_to_save | add_tag: order_location %}
    {% 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.