Auto-tag orders by sales channel
Use this task to tag orders as they come in, based on which sales channel created them. Run this task manually to backfill tags for customers based on their historical orders.
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
What you choose
Sales channel names and tags
Documentation
Use this task to tag orders as they come in, based on which sales channel created them. Run this task manually to backfill tags for customers based on their historical orders.
Please note: this task works by sales channel, and as such does not apply to orders that are created by an app. It's sometimes difficult to distinguish whether a third-party is using a sales channel or an app; if you're having trouble with this task, try its companion, Auto-tag orders by app.
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
Option types and flags
- Sales channel names and tags (keyval, required)
Task code
{% unless event.preview %}
{% capture query %}
query {
publications(first: 250) {
edges {
node {
id
name
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% assign available_channels = result.data.publications.edges | map: "node" | map: "name" %}
{% assign sales_channel_names = array %}
{% for keyval in options.sales_channel_names_and_tags__keyval_required %}
{% assign sales_channel_names[sales_channel_names.size] = keyval.first %}
{% endfor %}
{% endunless %}
{% assign orders = array %}
{% if event.topic contains "shopify/orders/" %}
{% capture query %}
query {
order(id: {{ order.admin_graphql_api_id | json }}) {
id
name
tags
publication {
name
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"order": {
"id": "gid://shopify/Order/1234567890",
"name": "#1234",
"tags": [],
"publication": {
"name": {{ options.sales_channel_names_and_tags__keyval_required.first.first | json }}
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign orders[orders.size] = result.data.order %}
{% elsif event.topic == "mechanic/user/trigger" %}
{% capture bulk_operation_query %}
query {
orders {
edges {
node {
__typename
id
name
tags
publication {
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 bulkOperation_json %}
{
"objects": [
{
"__typename": "Order",
"id": "gid://shopify/Order/1234567890",
"name": "#1234",
"tags": [],
"publication": {
"name": {{ options.sales_channel_names_and_tags__keyval_required.first.first | json }}
}
}
]
}
{% endcapture %}
{% assign bulkOperation = bulkOperation_json | parse_json %}
{% endif %}
{% assign orders = bulkOperation.objects %}
{% endif %}
{% for order in orders %}
{% assign publication_name = order.publication.name %}
{% if publication_name == blank %}
{% log order_id: order.id, order_name: order.name, message: "No sales channel present for this order; it was probably created by an app (see https://usemechanic.com/task/auto-tag-orders-by-app)." %}
{% continue %}
{% endif %}
{% assign tag_to_add = options.sales_channel_names_and_tags__keyval_required[publication_name] %}
{% if tag_to_add == blank %}
{% log order_id: order.id, order_name: order.name, message: "No tag configured for this channel. Skipping.", publication_name: publication_name, available_channels: available_channels %}
{% elsif result.data.order.tags contains tag_to_add %}
{% log order_id: order.id, order_name: order.name, message: "The order already has the tag for this channel. Skipping.", publication_name: publication_name, tag_to_add: tag_to_add %}
{% else %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ order.id | json }}
tags: {{ tag_to_add | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endfor %}
Mechanic Liquid reference
Default option values
- Sales channel names and tags
{"Online Store" => "online-store", "Buy Button" => "buy-button"}