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

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

Auto-tag customers with the location of their purchase

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 Occurs whenever an order is created, Occurs when a user manually triggers the task, and Occurs when a bulk operation is completed. Configuration includes tag for online orders.

15-day free trial – unlimited tasks

Documentation

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.

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 orders that have no physical location.

Run this task manually to have Mechanic scan your entire customer base, and each customer's order history.

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
mechanic/shopify/bulk_operation
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
tag for online orders
Code
{% if event.topic contains "shopify/orders" %}
  {% if event.preview %}
    {% capture order_json %}
      {
        "location": {
          "name": "Storefront"
        },
        "customer": {
          "admin_graphql_api_id": "gid://shopify/Customer/1234567890",
          "tags": ""
        }
      }
    {% endcapture %}

    {% assign order = order_json | parse_json %}
  {% endif %}

  {% assign customer_tags = order.customer.tags | split: ", " %}

  {% assign location = order.location.name | default: options.tag_for_online_orders %}

  {% unless location == blank or customer_tags contains location %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: {{ order.customer.admin_graphql_api_id | json }}
          tags: [{{ location | json }}]
        ) {
          node {
            ... on Customer {
              tags
            }
          }
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endunless %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% capture bulk_operation_query %}
    query {
      customers(query: "orders_count:>0") {
        edges {
          node {
            __typename
            id
            tags
            orders {
              edges {
                node {
                  __typename
                  id
                  physicalLocation {
                    name
                  }
                }
              }
            }
          }
        }
      }
    }
  {% endcapture %}

  {% action "shopify" %}
    mutation {
      bulkOperationRunQuery(
        query: {{ bulk_operation_query | json }}
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
  {% if event.preview %}
    {% capture objects_jsonl %}
      {"__typename":"Customer","id":"gid:\/\/shopify\/Customer\/1234567890","tags":[]}
      {"__typename":"Order","id":"gid:\/\/shopify\/Order\/1234567890","physicalLocation":{"name":"Storefront"},"__parentId":"gid:\/\/shopify\/Customer\/1234567890"}
    {% endcapture %}

    {% assign bulkOperation = hash %}
    {% assign bulkOperation["objects"] = objects_jsonl | parse_jsonl %}
  {% endif %}

  {% assign customers = bulkOperation.objects | where: "__typename", "Customer" %}
  {% assign orders = bulkOperation.objects | where: "__typename", "Order" %}

  {% for customer in customers %}
    {% assign customer_orders = orders | where: "__parentId", customer.id %}
    {% assign tags_to_add = array %}

    {% for order in customer_orders %}
      {% assign location = order.physicalLocation.name | default: options.tag_for_online_orders %}

      {% if location == blank or customer.tags contains location %}
        {% continue %}
      {% endif %}

      {% assign tags_to_add[tags_to_add.size] = location %}
    {% endfor %}

    {% if tags_to_add != empty %}
      {% action "shopify" %}
        mutation {
          tagsAdd(
            id: {{ customer.id | json }}
            tags: {{ tags_to_add | uniq | json }}
          ) {
            node {
              ... on Customer {
                tags
              }
            }
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}
  {% endfor %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more