Auto-tag orders with a cart attribute
This task checks for incoming orders, watching for orders that have a specific cart attribute filled in. When present, the task then copies that attribute over to the order's tags.
Try this taskWhen it runs
Occurs whenever an order is created
What you choose
Cart attribute to monitor
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
How task subscriptions work
Option types and flags
- Cart attribute to monitor (required)
Task code
{% if event.preview %}
{% capture order_json %}
{
"admin_graphql_api_id": "gid://shopify/Order/12345",
"tags": "",
"note_attributes": {
{{ options.cart_attribute_to_monitor__required | json }}: "Hello!"
}
}
{% endcapture %}
{% assign order = order_json | parse_json %}
{% endif %}
{% assign order_tags = order.tags | split: ", " %}
{% assign attribute_name = options.cart_attribute_to_monitor__required %}
{% assign attribute_value = order.note_attributes[attribute_name] %}
{% if attribute_value == blank %}
{% log "Attribute is not present" %}
{% elsif order_tags contains attribute_value %}
{% log message: "Order is already tagged", tag: attribute_value %}
{% else %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ order.admin_graphql_api_id | json }}
tags: {{ attribute_value | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
Mechanic Liquid reference