Auto-tag new orders by staff member, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Auto-tag new orders by staff member

by Mechanic Team (team@usemechanic.com)

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

Runs when an order is created and when a draft order is created. Configuration includes tag orders, tag draft orders, and staff names and tags.

15-day free trial – unlimited tasks

Documentation

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.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Gurus, everybody.

That’s why we make it easy to configure automation without code, why we make it easy to tweak the underlying code once tasks are installed, and why we publish it all here for everyone to learn from.

Events
when an order is created (shopify/orders/create)
when a draft order is created (shopify/draft_orders/create)
Options
tag orders (boolean), tag draft orders (boolean), staff names and tags (keyval, required)
Script
{% 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": "No staff member detected."}
  {"log": {{ result.data.first.last | json }}}
{% elsif tag_to_add == blank %}
  {"log": {{ "No tag applicable for " | append: name | json }}}
{% else %}
  {% action "shopify" %}
    mutation {
      tagsAdd(
        id: {{ resource_id | json }}
        tags: {{ tag_to_add | json }}
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Tag orders
true
Tag draft orders
true
Staff names and tags
{"Jane Doe"=>"created-by-jane", "Zhang Wei"=>"created-by-zhang"}