Auto-tag orders with product vendors
Use this task to automatically tag incoming orders with the vendor of each product in the order.
Try this taskWhen it runs
- Occurs whenever an order is created
- Occurs when a user sends an order to Mechanic
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/order
How task subscriptions work
Task code
{% if event.preview %}
{% capture order_json %}
{
"admin_graphql_api_id": "gid://shopify/Order/12345",
"tags": "Apple",
"line_items": [
{
"vendor": "Apple"
},
{
"vendor": "Nike"
},
{
"vendor": null
}
]
}
{% endcapture %}
{% assign order = order_json | parse_json %}
{% endif %}
{% assign product_vendors = order.line_items | map: "vendor" %}
{% assign order_tags = order.tags | split: ", " %}
{% assign tags_to_add = array %}
{% for vendor in product_vendors %}
{% if vendor == blank or order_tags contains vendor %}
{% continue %}
{% endif %}
{% assign tags_to_add[tags_to_add.size] = vendor %}
{% endfor %}
{% if tags_to_add != blank %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ order.admin_graphql_api_id | json }}
tags: {{ tags_to_add | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
Mechanic Liquid reference