Auto-tag orders created from drafts, with Mechanic.

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

Auto-tag orders created from drafts

This task monitors for newly-created orders, and auto-tags any that were originally based on a Shopify draft order, using the tag of your choice.

Runs Occurs whenever an order is created. Configuration includes tag to add.

15-day free trial – unlimited tasks

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
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
tag to add (required)
Code
{% assign order_qualifies = false %}

{% if order.source_name == "shopify_draft_order" %}
  {% assign order_qualifies = true %}
{% elsif order.source_name == "1830279" %}
  {% comment %}
    1830279 is the ID of the "Shopify Web" app. If we're seeing it here, it means that
    the order was created via the /admin area.
  {% endcomment %}

  {% capture query %}
    query {
      order(id: {{ order.admin_graphql_api_id | json }}) {
        app {
          name
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "order": {
            "app": {
              "name": "Draft Orders"
            }
          }
        }
      }
    {% endcapture %}

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

  {% if result.data.order.app.name == "Draft Orders" %}
    {% assign order_qualifies = true %}
  {% else %}
    {% log message: "Order does not qualify",
        order_source_name: order.source_name,
        order_app_name: result.data.order.app.name %}
  {% endif %}
{% else %}
  {% log message: "Order does not qualify",
      order_source_name: order.source_name %}
{% endif %}

{% if order_qualifies %}
  {% action "shopify" %}
    mutation {
      tagsAdd(
        id: {{ order.admin_graphql_api_id | json }}
        tags: {{ options.tag_to_add__required | json }}
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Tag to add
from-draft