Mechanic Get Mechanic
Task library

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.

Try this task

When it runs

Occurs whenever an order is created

What you choose

  • Order attribute name for storing the draft order ID
  • Order attribute name for storing the draft order name
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
How task subscriptions work

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

Default option values

Order attribute name for storing the draft order ID
Draft Order ID
Order attribute name for storing the draft order name
Draft Order Name