Auto-tag customers with vendors after ordering, with Mechanic.

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

Auto-tag customers with vendors after ordering

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

15-day free trial – unlimited tasks

Documentation

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. :)

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, 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
Code
{% if event.topic == "shopify/orders/create" %}
  {% assign customer = order.customer.reload %}

  {% if event.preview %}
    {% capture order_json %}
      {
        "customer": {
          "admin_graphql_api_id": "gid://shopify/Customer/1234567890",
          "tags": "ACME"
        },
        "line_items": [
          {
            "vendor": "ABC Company"
          },
          {
            "vendor": "ACME"
          }
        ]
      }
    {% endcapture %}

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

  {% assign customer_tags = customer.tags | split: ", " %}
  {% assign vendors = order.line_items | map: "vendor" | uniq %}

  {% assign tags_to_add = array %}

  {% for vendor in vendors %}
    {% unless customer_tags contains vendor %}
      {% assign tags_to_add[tags_to_add.size] = vendor %}
    {% endunless%}
  {% endfor %}

  {% if tags_to_add != empty %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: {{ customer.admin_graphql_api_id | json }}
          tags: {{ tags_to_add | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}

{% elsif event.topic == "mechanic/user/trigger" %}
  {% capture bulk_operation_query %}
    query {
      customers {
        edges {
          node {
            __typename
            id
            tags
            orders {
              edges {
                node {
                  __typename
                  id
                  lineItems {
                    edges {
                      node {
                        __typename
                        id
                        vendor
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  {% 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 bulkOperation_objects_jsonl %}
      {"__typename":"Customer","id":"gid:\/\/shopify\/Customer\/1234567890","tags":["ACME"]}
      {"__typename":"Order","id":"gid:\/\/shopify\/Order\/1234567890","__parentId":"gid:\/\/shopify\/Customer\/1234567890"}
      {"__typename":"LineItem","id":"gid:\/\/shopify\/LineItem\/1234567890","vendor":"ABC Company","__parentId":"gid:\/\/shopify\/Order\/1234567890"}
      {"__typename":"LineItem","id":"gid:\/\/shopify\/LineItem\/2345678901","vendor":"ACME","__parentId":"gid:\/\/shopify\/Order\/1234567890"}
    {% endcapture %}

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

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

  {% for customer in customers %}
    {% assign vendors = array %}
    {% assign tags_to_add = array %}

    {% assign customer_orders = bulkOperation.objects | where: "__typename", "Order" | where: "__parentId", customer.id %}

    {% for order in customer_orders %}
      {% assign order_vendors = bulkOperation.objects | where: "__typename", "LineItem" | where: "__parentId", order.id | map: "vendor" | uniq %}
      {% assign vendors = vendors | concat: order_vendors %}
    {% endfor %}

    {% for vendor in vendors %}
      {% unless customer.tags contains vendor %}
        {% assign tags_to_add[tags_to_add.size] = vendor %}
      {% endunless%}
    {% endfor %}

    {% assign tags_to_add = tags_to_add | uniq %}

    {% if tags_to_add != empty %}
      {% action "shopify" %}
        mutation {
          tagsAdd(
            id: {{ customer.id | json }}
            tags: {{ tags_to_add | json }}
          ) {
            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