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. :)
Try this taskWhen it runs
- Occurs whenever an order is created
- Occurs when a user manually triggers the task
- Occurs when a bulk operation is completed
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.
Task code and subscriptions
You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.
Subscriptions
shopify/orders/create
mechanic/user/trigger
mechanic/shopify/bulk_operation
How task subscriptions work
Task 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 %}
Mechanic Liquid reference