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

Mechanic is a development and ecommerce automation platform for Shopify. :)

Auto-tag orders by originating staff member

Use this task to keep track of who created what order. 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.

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

15-day free trial – unlimited tasks

Documentation

Use this task to keep track of who created what order. 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.

Optionally, enter a tag prefix to help identify these tags (e.g "Placed by: ").

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, 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.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
shopify/orders/create
mechanic/user/trigger
mechanic/shopify/bulk_operation
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
tag prefix, include pos orders (boolean)
Code
{% assign tag_prefix = options.tag_prefix %}
{% assign include_pos_orders = options.include_pos_orders__boolean %}

{% if event.topic == "shopify/orders/create" %}
  {% capture query %}
    query {
      order(id: {{ order.admin_graphql_api_id | json }}) {
        id
        name
        sourceName
        tags
        events(
          first: 1
          query: "action:placed"
        ) {
          nodes {
            attributeToUser
            message
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "order": {
            "id": "gid://shopify/Order/1234567890",
            "events": {
              "nodes": [
                {
                  "attributeToUser": true,
                  "message": "Jean Deaux created this order."
                }
              ]
            }
          }
        }
      }
    {% endcapture %}

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

  {% assign order = result.data.order %}
  {% assign placed_event = order.events.nodes.first %}
  {% assign originator = nil %}

  {% comment %}
    -- the event message will vary based on the order source (draft order vs. POS)
  {% endcomment %}

  {% if order.sourceName == "shopify_draft_order" %}
    {% if placed_event.attributeToUser and placed_event.message contains " created " %}
      {% assign originator = placed_event.message | split: " created " | first %}
    {% endif %}

  {% elsif order.sourceName == "pos" and include_pos_orders %}
    {% if placed_event.message contains " processed this order" %}
      {% assign originator = placed_event.message | split: " processed " | first %}
    {% endif %}
  {% endif %}

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

    {% unless order.tags contains originator_tag %}
      {% action "shopify" %}
        mutation {
          tagsAdd(
            id: {{ order.id | json }}
            tags: {{ originator_tag | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endunless %}
  {% endif %}

{% elsif event.topic == "mechanic/user/trigger" %}
  {% comment %}
    -- get all orders created from draft or via POS
  {% endcomment %}

  {% capture bulk_operation_query %}
    query {
      orders(
        query: "source_name:pos OR source_name:shopify_draft_order"
      ) {
        edges {
          node {
            __typename
            id
            name
            sourceName
            tags
            events(
              first: 1
              query: "action:placed"
            ) {
              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 %}
    {% capture jsonl_string %}
      {"__typename":"Order","id":"gid://shopify/Order/1234567890","sourceName":"shopify_draft_order"}
      {"__typename":"BasicEvent","attributeToUser":true,"message":"Jean Deaux created this order","__parentId":"gid://shopify/Order/1234567890"}
      {"__typename":"Order","id":"gid://shopify/Order/2345678901","sourceName":"pos"}
      {"__typename":"BasicEvent","attributeToUser":false,"message":"Jean Deaux processed this order","__parentId":"gid://shopify/Order/2345678901"}
    {% endcapture %}

    {% assign bulkOperation = hash %}
    {% assign bulkOperation["objects"] = jsonl_string | parse_jsonl %}
  {% endif %}

  {% assign orders = bulkOperation.objects | where: "__typename", "Order" %}
  {% assign events = bulkOperation.objects | where: "__typename", "BasicEvent" %}

  {% for order in orders %}
    {% assign placed_event = events | where: "__parentId", order.id | first %}
    {% assign originator = nil %}

    {% if order.sourceName == "shopify_draft_order" %}
      {% if placed_event.attributeToUser and placed_event.message contains " created " %}
        {% assign originator = placed_event.message | split: " created " | first %}
      {% endif %}

    {% elsif order.sourceName == "pos" and include_pos_orders %}
      {% if placed_event.message contains " processed this order" %}
        {% assign originator = placed_event.message | split: " processed " | first %}
      {% endif %}
    {% endif %}

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

      {% unless order.tags contains originator_tag %}
        {% action "shopify" %}
          mutation {
            tagsAdd(
              id: {{ order.id | json }}
              tags: {{ originator_tag | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          }
        {% endaction %}
      {% endunless %}
    {% endif %}
  {% endfor %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more