Auto-add the draft order ID to an order's attributes, with Mechanic.

Mechanic is a development platform for Shopify. :)

Auto-add the draft order ID to an order's attributes

by Isaac Bowen (team@usemechanic.com)

When a new order is created from a draft order, this task will automatically add the draft order ID to the custom attributes of the new order.

Runs when an order is created. Configuration includes order attribute name for storing the draft order.

15-day free trial – unlimited tasks

Documentation

When a new order is created from a draft order, this task will automatically add the draft order ID to the custom attributes of the new order.

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)
Options
order attribute name for storing the draft order (required)
Script
{% if event.preview %}
  {% assign order = hash %}
  {% assign order["admin_graphql_api_id"] = "gid://shopify/Order/1234567890" %}
  {% assign order["note_attributes"] = array %}
  {% assign order["note_attributes"][0] = hash %}
  {% assign order["note_attributes"][0]["name"] = "Existing attribute name" %}
  {% assign order["note_attributes"][0]["value"] = "Existing attribute value" %}
{% endif %}

{% capture query %}
  query {
    order(id: {{ order.admin_graphql_api_id | json }}) {
      events(first: 1, sortKey: CREATED_AT) {
        edges {
          node {
            message
          }
        }
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "order": {
          "events": {
            "edges": [
              {
                "node": {
                  "message": "Staff Member created this order from draft order <a href=\"https://example.myshopify.com/admin/draft_orders/1234567890\">#1000</a>."
                }
              }
            ]
          }
        }
      }
    }
  {% endcapture %}

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

{% assign event_message = result.data.order.events.edges.first.node.message %}
{% if event_message contains "created this order from draft order" %}
  {% assign draft_order_id = event_message | split: "/draft_orders/" | last | split: '"' | first %}
  {% if draft_order_id == blank %}
    {% log message: "Couldn't locate the draft order ID", event_message: event_message %}
  {% else %}
    {% action "shopify" %}
      mutation {
        orderUpdate(
          input: {
            id: {{ order.admin_graphql_api_id | json }}
            customAttributes: [
              {% for note_attribute in order.note_attributes %}
                {
                 key: {{ note_attribute.name | json }}
                 value: {{ note_attribute.value | json }}
                },
              {% endfor %}
              {
                key: {{ options.order_attribute_name_for_storing_the_draft_order_id__required | json }}
                value: {{ draft_order_id | json }}
              }
            ]
          }
        ) {
          order {
            customAttributes {
              key
              value
            }
          }
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% else %}
  {% log message: "Event message didn't mention a draft order", event_message: event_message %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Order attribute name for storing the draft order
Draft order ID