Auto-tag orders using product tags
Use this task to tag incoming orders with all the product tags in the order. Optionally, specify a specific list of tags to be copied, and/or a certain tag prefix to watch for. Can be run manually, to scan and tag 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
- Occurs when a user sends an order to Mechanic
What you choose
- Only copy these tags
- Only copy tags having one of these prefixes
Documentation
Use this task to tag incoming orders with all the product tags in the order. Optionally, specify a specific list of tags to be copied, and/or a certain tag prefix to watch for. Can be run manually, to scan and tag historical orders.
Use this task to tag incoming orders with all the product tags in the order. Optionally, specify a specific list of tags to be copied, and/or a certain tag prefix to watch for.
Run this task manually to scan and tag all orders, in bulk.
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
mechanic/user/order
How task subscriptions work
Option types and flags
- Only copy these tags (array)
- Only copy tags having one of these prefixes (array)
Task code
{% assign only_copy_these_tags = options.only_copy_these_tags__array %}
{% assign only_copy_tags_having_one_of_these_prefixes = options.only_copy_tags_having_one_of_these_prefixes__array %}
{% comment %}
-- check configured tags to make sure no inadvertent spaces are present
{% endcomment %}
{% for tag in only_copy_these_tags %}
{% assign tag_check = tag | strip %}
{% if tag_check == "" %}
{% error "'Only copy these tags' contains an empty entry. Please correct to continue." %}
{% endif %}
{% endfor %}
{% for tag in only_copy_tags_having_one_of_these_prefixes %}
{% assign tag_check = tag | strip %}
{% if tag_check == "" %}
{% error "'Only copy tags having one of these prefixes' contains an empty entry. Please correct to continue." %}
{% endif %}
{% endfor %}
{% assign order_ids_tags_and_product_tags = array %}
{% if event.topic == "shopify/orders/create" or event.topic == "mechanic/user/order" %}
{% comment %}
-- query the order, line items, products, and tags
{% endcomment %}
{% capture query %}
query {
order(id: {{ order.admin_graphql_api_id | json }}) {
id
tags
lineItems(first: 250) {
nodes {
product {
tags
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"order": {
"id": "gid://shopify/Order/1234567890",
"lineItems": {
"nodes": [
{
"product": {
"tags": [
"preview-tag",
{{ only_copy_these_tags.first | json }},
{{ only_copy_tags_having_one_of_these_prefixes.first | json }}
]
}
}
]
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign order = result.data.order %}
{% comment %}
-- save order ID and tags as the only item in the array for processing
{% endcomment %}
{% assign item = array %}
{% assign item[0] = order.id %}
{% assign item[1] = order.tags %}
{% assign item[2] = order.lineItems.nodes | map: "product" | map: "tags" | uniq %}
{% assign order_ids_tags_and_product_tags[0] = item %}
{% elsif event.topic == "mechanic/user/trigger" %}
{% comment %}
-- use bulk op to get all orders in the shop
{% endcomment %}
{% capture bulk_operation_query %}
query {
orders {
edges {
node {
__typename
id
tags
lineItems {
edges {
node {
__typename
product {
tags
}
}
}
}
}
}
}
}
{% 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":"Order","id":"gid:\/\/shopify\/Order\/1234567890","tags":[]}
{"__typename":"LineItem","__parentId":"gid:\/\/shopify\/Order\/1234567890","product":{"tags":["preview-tag",{{ only_copy_these_tags.first | json }},{{ only_copy_tags_having_one_of_these_prefixes.first | json }}]}}
{% endcapture %}
{% assign bulkOperation = hash %}
{% assign bulkOperation["objects"] = objects_jsonl | parse_jsonl %}
{% endif %}
{% assign orders = bulkOperation.objects | where: "__typename", "Order" %}
{% assign line_items = bulkOperation.objects | where: "__typename", "LineItem" %}
{% comment %}
-- save order IDs and tags in array for processing
{% endcomment %}
{% for order in orders %}
{% assign order_product_tags
= line_items
| where: "__parentId", order.id
| map: "product"
| map: "tags"
| uniq
%}
{% assign item = array %}
{% assign item[0] = order.id %}
{% assign item[1] = order.tags %}
{% assign item[2] = order_product_tags %}
{% assign order_ids_tags_and_product_tags[order_ids_tags_and_product_tags.size] = item %}
{% endfor %}
{% endif %}
{% comment %}
-- process items to see which tags to add to each order
{% endcomment %}
{% for item in order_ids_tags_and_product_tags %}
{% assign order_id = item[0] %}
{% assign order_tags = item[1] %}
{% assign order_product_tags = item[2] %}
{% assign tags_to_add = array %}
{% for product_tag in order_product_tags %}
{% if order_tags contains product_tag %}
{% continue %}
{% endif %}
{% if only_copy_these_tags == blank and only_copy_tags_having_one_of_these_prefixes == blank %}
{% assign tags_to_add = tags_to_add | push: product_tag %}
{% continue %}
{% endif %}
{% for tag in only_copy_these_tags %}
{% if tag == product_tag %}
{% assign tags_to_add = tags_to_add | push: product_tag %}
{% endif %}
{% endfor %}
{% comment %}
-- make sure the prefix matches the beginning of the tag
{% endcomment %}
{% for tag in only_copy_tags_having_one_of_these_prefixes %}
{% assign prefix_length = tag.size %}
{% assign product_tag_substr = product_tag | slice: 0, prefix_length %}
{% if tag == product_tag_substr %}
{% assign tags_to_add = tags_to_add | push: product_tag %}
{% endif %}
{% endfor %}
{% endfor %}
{% comment %}
-- remove duplicate tags before adding to order
{% endcomment %}
{% assign tags_to_add = tags_to_add | compact | uniq %}
{% if tags_to_add != blank %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ order_id | json }}
tags: {{ tags_to_add | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endfor %}
Mechanic Liquid reference