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

Mechanic is an automation development platform for Shopify. :)

Auto-tag orders by originating staff member

by Isaac Bowen (team@usemechanic.com)

Use this task to keep track of who created what order. This task watches for new orders, and tags each one with the name of the store staff member who created it (if that's how the order was created). This task can also be run manually, to scan and tag all orders already in your store.

Runs when an order is created, when a user triggers the task, and when a bulk operation finishes. Configuration includes include pos orders and tag prefix.

15-day free trial – unlimited tasks

Documentation

This task watches for new orders, that are created by store staff members, auto-tagging each new order with the name of the user who created it. Run this task manually to scan and tag all existing orders in your store.

YouTube: Watch the development video!

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 user triggers the task (mechanic/user/trigger)
when a bulk operation finishes (mechanic/shopify/bulk_operation)
Options
include pos orders (boolean), tag prefix
Script
{% if event.topic == "shopify/orders/create" %}
  {% capture query %}
    query {
      order(id: {{ 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": {
          "order": {
            "id": "gid://shopify/Order/1234567890",
            "tags": [],
            "events": {
              "edges": [
                {
                  "node": {
                    "attributeToUser": true,
                    "message": "Sam Smith created this order."
                  }
                }
              ]
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = result_json | parse_json %}
  {% endif %}

  {% assign orderNode = result.data.order %}
  {% assign eventNode = orderNode.events.edges.first.node %}
  {% assign originator = nil %}

  {% if eventNode.attributeToUser and eventNode.message contains " created " %}
    {% assign originator = eventNode.message | split: " created " | first %}
  {% elsif options.include_pos_orders__boolean and eventNode.attributeToUser == false and eventNode.message contains " processed this order on Shopify POS." %}
    {% assign originator = eventNode.message | split: " processed " | first %}
  {% endif %}

  {% if originator %}
    {% assign originator_tag = options.tag_prefix | append: originator %}
    {% unless orderNode.tags contains originator_tag %}
      {% action "shopify" %}
        mutation {
          tagsAdd(
            id: {{ orderNode.id | json }}
            tags: {{ originator_tag | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endunless %}
  {% endif %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% capture bulk_operation_query %}
    query {
      orders {
        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": "Order",
          "id": "gid://shopify/Order/1234567890",
          "tags": []
        },
        {
          "__typename": "BasicEvent",
          "id": "gid://shopify/BasicEvent/1234567890",
          "attributeToUser": true,
          "message": "Sam Smith created this order from draft order <a href=\"https://example.myshopify.com/admin/draft_orders/1234567890\">#D1</a>.",
          "__parentId": "gid://shopify/Order/1234567890",
          "__parent": {
            "__typename": "Order",
            "id": "gid://shopify/Order/1234567890",
            "tags": []
          }
        },
        {
          "__typename": "BasicEvent",
          "id": "gid://shopify/BasicEvent/2345678901",
          "attributeToUser": false,
          "message": "Received new order <a href=\"https://mechanic-demo.myshopify.com/admin/orders/1835132911651\">#1001</a>.",
          "__parentId": "gid://shopify/Order/1234567890"
        }
      ]
    {% endcapture %}

    {% assign bulkOperation["objects"] = objects_json | parse_json %}
  {% endif %}

  {% assign eventNodes = bulkOperation.objects | where: "__typename", "BasicEvent" %}

  {% for eventNode in eventNodes %}
    {% assign originator = nil %}

    {% if eventNode.attributeToUser and eventNode.message contains " created " %}
      {% assign originator = eventNode.message | split: " created " | first %}
    {% elsif options.include_pos_orders__boolean and eventNode.attributeToUser == false and eventNode.message contains " processed this order on Shopify POS." %}
      {% assign originator = eventNode.message | split: " processed " | first %}
    {% endif %}

    {% if originator %}
      {% assign originator_tag = options.tag_prefix | append: originator %}

      {% assign orderNode = eventNode.__parent %}

      {% unless orderNode.tags contains originator_tag %}
        {% action "shopify" %}
          mutation {
            tagsAdd(
              id: {{ orderNode.id | json }}
              tags: {{ originator_tag | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          }
        {% endaction %}
      {% endunless %}
    {% endif %}
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.