Mechanic Get Mechanic
Task library

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.

Try this task

When it runs

  • Occurs whenever an order is created
  • Occurs when a user manually triggers the task
  • Occurs when a bulk operation is completed

What you choose

  • Tag prefix
  • Include POS orders

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: ").

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/orders/create
mechanic/user/trigger
mechanic/shopify/bulk_operation
How task subscriptions work

Option types and flags

Task 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 %}
Mechanic Liquid reference