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

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

Auto-tag draft orders by originating staff member

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

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

15-day free trial – unlimited tasks

Documentation

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

Optionally, enter a tag prefix to help identify these tags (e.g "Created 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/draft_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
Code
{% assign tag_prefix = options.tag_prefix %}

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

  {% assign result = query | shopify %}

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

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

  {% assign draft_order = result.data.draftOrder %}
  {% assign create_event = draft_order.events.nodes.first %}

  {% if create_event.attributeToUser and create_event.message contains " created " %}
    {% assign originator = create_event.message | split: " created " | first %}
    {% assign originator_tag = tag_prefix | append: originator %}

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

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

  {% capture bulk_operation_query %}
    query {
      draftOrders {
        edges {
          node {
            __typename
            id
            tags
            events(
              first: 1
              query: "action:create"
            ) {
              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":"DraftOrder","id":"gid://shopify/DraftOrder/1234567890"}
      {"__typename":"BasicEvent","attributeToUser":true,"message":"Jean Deaux created this draft order","__parentId":"gid://shopify/DraftOrder/1234567890"}
    {% endcapture %}

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

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

  {% for draft_order in draft_orders %}
    {% assign create_event = events | where: "__parentId", draft_order.id | first %}

    {% if create_event.attributeToUser and create_event.message contains " created " %}
      {% assign originator = create_event.message | split: " created " | first %}
      {% assign originator_tag = tag_prefix | append: originator %}

      {% unless draft_order.tags contains originator_tag %}
        {% action "shopify" %}
          mutation {
            tagsAdd(
              id: {{ draft_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