Auto-tag customers with vendors after ordering with Mechanic.

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

Auto-tag customers with vendors after ordering

by Isaac Bowen (team@usemechanic.com)

This task tags customers with the name of each vendor in their order, as soon as the order is created. Useful for keeping track of which vendors a customer is interested in. :)

Runs when an order is created and when a user triggers the task.

15-day free trial – unlimited tasks

Documentation

This task will run for each new order that's created, applying product vendors as customer tags.

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

  {% assign vendors = order.line_items | map: "vendor" | sort | uniq %}

  {% assign customer_tags_to_save = customer.tags | add_tags: vendors %}

  {% 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 %}
      {% assign vendors = order.line_items | map: "vendor" | uniq %}
      {% assign customer_tags_to_save = customer_tags_to_save | add_tags: vendors %}
    {% 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.