Auto-add the draft order to a new order's attributes, with Mechanic.

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

Auto-add the draft order to a new order's attributes

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

Runs Occurs whenever an order is created. Configuration includes order attribute name for storing the draft order id and order attribute name for storing the draft order name.

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
order attribute name for storing the draft order id, order attribute name for storing the draft order name
Code
{% if options.order_attribute_name_for_storing_the_draft_order_id == blank and options.order_attribute_name_for_storing_the_draft_order_name == blank %}
  {% error "Please fill in at least one order attribute name." %}
{% endif %}

{% 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 ".myshopify.com/admin/draft_orders/" %}
  {% assign draft_order_id = event_message | split: "/draft_orders/" | last | split: '"' | first %}
  {% assign draft_order_name = event_message | split: ">#" | last | split: "</a>" | first | prepend: "#" %}

  {% 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 %}
              {% if options.order_attribute_name_for_storing_the_draft_order_id != blank %}
                {
                  key: {{ options.order_attribute_name_for_storing_the_draft_order_id | json }}
                  value: {{ draft_order_id | json }}
                }
              {% endif %}
              {% if options.order_attribute_name_for_storing_the_draft_order_name != blank %}
                {
                  key: {{ options.order_attribute_name_for_storing_the_draft_order_name | json }}
                  value: {{ draft_order_name | json }}
                }
              {% endif %}
            ]
          }
        ) {
          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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Order attribute name for storing the draft order ID
Draft Order ID
Order attribute name for storing the draft order name
Draft Order Name