Auto-tag draft orders by originating staff member
Use this task to keep track of who created what draft order. This task watches for new draft orders, and tags each one with the name of the store staff member who created it. This task can also be run manually, to scan and tag all draft orders already in your store.
Try this taskWhen it runs
- Occurs whenever a draft order is created
- Occurs when a user manually triggers the task
- Occurs when a bulk operation is completed
What you choose
Tag prefix
Documentation
Use this task to keep track of who created what draft order. This task watches for new draft orders, and tags each one with the name of the store staff member who created it. This task can also be run manually, to scan and tag all draft orders already in your store.
This task watches for new draft orders, that are created by store staff members, auto-tagging each new draft order with the name of the user who created it. Run this task manually to scan and tag all existing draft orders in your store.
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/draft_orders/create
mechanic/user/trigger
mechanic/shopify/bulk_operation
How task subscriptions work
Task code
{% if event.topic == "shopify/draft_orders/create" %}
{% capture query %}
query {
draftOrder(id: {{ draft_order.admin_graphql_api_id | json }}) {
id
tags
events(first: 1, sortKey: CREATED_AT) {
edges {
node {
attributeToUser
message
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"draftOrder": {
"id": "gid://shopify/DraftOrder/1234567890",
"tags": [],
"events": {
"edges": [
{
"node": {
"attributeToUser": true,
"message": "Sam Smith created this draft order."
}
}
]
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign draftOrderNode = result.data.draftOrder %}
{% assign eventNode = draftOrderNode.events.edges.first.node %}
{% if eventNode.attributeToUser and eventNode.message contains " created " %}
{% assign originator = eventNode.message | split: " created " | first %}
{% assign originator_tag = options.tag_prefix | append: originator %}
{% unless draftOrderNode.tags contains originator_tag %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ draftOrderNode.id | json }}
tags: {{ originator_tag | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endunless %}
{% endif %}
{% elsif event.topic == "mechanic/user/trigger" %}
{% capture bulk_operation_query %}
query {
draftOrders {
edges {
node {
__typename
id
tags
events(sortKey: CREATED_AT) {
edges {
node {
__typename
id
attributeToUser
message
}
}
}
}
}
}
}
{% 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 %}
{% assign bulkOperation = hash %}
{% capture objects_json %}
[
{
"__typename": "DraftOrder",
"id": "gid://shopify/DraftOrder/1234567890",
"tags": []
},
{
"__typename": "BasicEvent",
"id": "gid://shopify/BasicEvent/1234567890",
"attributeToUser": true,
"message": "Sam Smith created this draft order.",
"__parentId": "gid://shopify/DraftOrder/1234567890",
"__parent": {
"__typename": "DraftOrder",
"id": "gid://shopify/DraftOrder/1234567890",
"tags": []
}
}
]
{% endcapture %}
{% assign bulkOperation["objects"] = objects_json | parse_json %}
{% endif %}
{% assign eventNodes = bulkOperation.objects | where: "__typename", "BasicEvent" | where: "attributeToUser", true %}
{% for eventNode in eventNodes %}
{% unless eventNode.message contains " created " %}
{% continue %}
{% endunless %}
{% assign originator = eventNode.message | split: " created " | first %}
{% assign originator_tag = options.tag_prefix | append: originator %}
{% assign draftOrderNode = eventNode.__parent %}
{% unless draftOrderNode.tags contains originator_tag %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ draftOrderNode.id | json }}
tags: {{ originator_tag | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endunless %}
{% endfor %}
{% endif %}
Mechanic Liquid reference