Mechanic Get Mechanic
Task library

Auto-tag new orders by staff member

Use this task to easily keep track of who's responsible for which orders. Works for draft orders, too!

Try this task

When it runs

  • Occurs whenever an order is created
  • Occurs whenever a draft order is created

What you choose

  • Tag orders
  • Tag draft orders
  • Staff names and tags

Documentation

Use this task to easily keep track of who's responsible for which orders. Works for draft orders, too!

To use this task, fill in the "Staff names and tags" option with staff names on the left, and the tags you'd like to apply on the right. Make sure to use each staff member's exact full name – it's case-sensitive! Tags will be applied as soon as the order or draft order is created.

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

{% if options.tag_orders__boolean %}shopify/orders/create{% endif %}
{% if options.tag_draft_orders__boolean %}shopify/draft_orders/create{% endif %}
How task subscriptions work

Option types and flags

Task code

{% unless options.tag_orders__boolean or options.tag_draft_orders__boolean %}
  {% error "Choose at least one resource to auto-tag." %}
{% endunless %}

{% if event.topic contains "shopify/orders/" %}
  {% assign resource_id = order.admin_graphql_api_id | default: "gid://shopify/Order/1234567890" %}
  {% assign connection = "order" %}
{% elsif event.topic contains "shopify/draft_orders/" %}
  {% assign resource_id = draft_order.admin_graphql_api_id | default: "gid://shopify/DraftOrder/1234567890" %}
  {% assign connection = "draftOrder" %}
{% endif %}

{% capture query %}
  query {
    {{ connection }}(id: {{ resource_id | json }}) {
      id
      events(first: 250) {
        edges {
          node {
            attributeToUser
            message
          }
        }
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% assign placed_event = result.data.first.last.events.edges | map: "node" | where: "attributeToUser" | first %}
{% assign name = placed_event.message | split: " created this " | first %}
{% assign tag_to_add = options.staff_names_and_tags__keyval_required[name] %}

{% if event.preview %}
  {% assign name = options.staff_names_and_tags__keyval_required.first.first %}
  {% assign tag_to_add = options.staff_names_and_tags__keyval_required.first.last %}
{% endif %}

{% if name == blank %}
  {% log message: "No staff member detected.", resource: result.data.first.last %}
{% elsif tag_to_add == blank %}
  {% log message: "No tag applicable for staff member.", staff_member: name %}
{% else %}
  {% action "shopify" %}
    mutation {
      tagsAdd(
        id: {{ resource_id | json }}
        tags: {{ tag_to_add | json }}
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% endif %}
Mechanic Liquid reference

Default option values

Tag orders
true
Tag draft orders
true
Staff names and tags
{"Jane Doe" => "created-by-jane", "Zhang Wei" => "created-by-zhang"}