Mechanic is a development and ecommerce automation platform for Shopify. :)
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.
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?)
shopify/orders/create
{% 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 %}
Draft Order ID
Draft Order Name