Mechanic is a development platform for Shopify. :)
This task looks up orders by their order number, adds the note of your choice to the order record, and removes that same note 12 hours later. A bespoke task for a very specific merchant need, we publish this task as an example of getting creative with Mechanic. :)
Runs when some text is submitted and when user/orders/remove_note is triggered. Configuration includes hours to wait and order note to add.
This task looks up orders by their order number, adds the note of your choice to the order record, and removes that same note 12 hours later. A bespoke task for a very specific merchant need, we publish this task as an example of getting creative with Mechanic. :)
Run this task manually, and enter one or more order numbers (one per line, making sure to use the exact order number - including symbols and prefixes/suffixes. Mechanic will add the note you configure to the corresponding order, and remove it 12 hours later.
Mechanic will preserve any existing notes on file for these orders, appending the new note content to the end of the content already in place.
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.
{% assign hours_to_wait_in_seconds = options.hours_to_wait__number_required | times: 60 | times: 60 %} {% if event.topic == "mechanic/user/text" %} {% if event.preview %} {% assign event = hash %} {% assign event["preview"] = true %} {% assign event["data"] = "#1234" %} {% endif %} {% assign order_numbers = event.data | strip | split: newline | sort | uniq %} {% assign order_number_params = array %} {% for number in order_numbers %} {% assign order_number_params[order_number_params.size] = number | json %} {% endfor %} {% assign orders = array %} {% assign cursor = nil %} {% for n in (0..100) %} {% capture query %} query { orders( first: 250 query: {{ order_number_params | join: " OR " | json }} after: {{ cursor | json }} ) { pageInfo { hasNextPage } edges { cursor node { id name note } } } } {% endcapture %} {% assign result = query | shopify %} {% if event.preview %} {% capture result_json %} { "data": { "orders": { "edges": [ { "node": { "id": "gid://shopify/Order/1234567890", "name": "#1234", "note": "Existing order note" } } ] } } } {% endcapture %} {% assign result = result_json | parse_json %} {% endif %} {% for order_edge in result.data.orders.edges %} {% assign order = order_edge.node %} {% if order_numbers contains order.name %} {% assign orders[orders.size] = order %} {% endif %} {% endfor %} {% if result.data.orders.pageInfo.hasNextPage %} {% assign cursor = result.data.orders.edges.last.cursor %} {% else %} {% break %} {% endif %} {% endfor %} {% assign found_order_numbers = orders | map: "name" | sort %} {% if found_order_numbers != order_numbers %} {% error %} { "message": "Tried to find {{ order_numbers.size }} order(s), found {{ found_order_numbers.size }} instead. Please check your input, and try again. Make sure to use the complete and exact order number, including any symbols and prefixes/suffixes (e.g. W#1234, not 1234).", "order_numbers": {{ order_numbers | json }}, "found_order_numbers": {{ found_order_numbers | json }} } {% enderror %} {% elsif orders != blank %} {% for order in orders %} {% capture order_note_to_save %}{% if order.note != blank %}{{ order.note }}{{ newline }}{{ newline }}{% endif %}{{ options.order_note_to_add__required }}{% endcapture %} {% log order: order %} {% action "shopify" %} mutation { orderUpdate( input: { id: {{ order.id | json }} note: {{ order_note_to_save | json }} } ) { userErrors { field message } } } {% endaction %} {% endfor %} {% action "event" %} { "topic": "user/orders/remove_note", "run_at": {{ "now" | date: "%s" | plus: hours_to_wait_in_seconds | round | json }}, "data": { "order_ids": {{ orders | map: "id" | json }}, "order_note_content_to_remove": {{ options.order_note_to_add__required | json }} } } {% endaction %} {% endif %} {% elsif event.topic == "user/orders/remove_note" %} {% if event.preview %} {% capture event_json %} { "preview": true, "data": { "order_ids": ["gid://shopify/Order/1234567890"], "order_note_content_to_remove": {{ options.order_note_to_add__required | json }} } } {% endcapture %} {% assign event = event_json | parse_json %} {% endif %} {% for order_id in event.data.order_ids %} {% capture query %} query { order(id: {{ order_id | json }}) { id name note } } {% endcapture %} {% assign result = query | shopify %} {% if event.preview %} {% capture result_json %} { "data": { "order": { "id": "gid://shopify/Order/1234567890", "name": "#1234", "note": {{ "Existing note!" | append: newline | append: newline | append: options.order_note_to_add__required | json }} } } } {% endcapture %} {% assign result = result_json | parse_json %} {% endif %} {% assign order = result.data.order %} {% if order == nil %} {% log message: "Order no longer exists", id: order_id %} {% continue %} {% endif %} {% assign order_note_parts = order.note | split: newline %} {% assign order_note_to_save = "" %} {% for order_note_part in order_note_parts %} {% unless order_note_part == options.order_note_to_add__required %} {% capture order_note_to_save %}{% if order_note_to_save != blank %}{{ order_note_to_save }}{{ newline }}{% endif %}{{ order_note_part }}{% endcapture %} {% endunless %} {% endfor %} {% assign order_note_to_save = order_note_to_save | strip %} {% if order_note_to_save == order.note %} {% log message: "Order note to save appears to match the existing order note", order: order, order_note_to_save: order_note_to_save %} {% else %} {% action "shopify" %} mutation { orderUpdate( input: { id: {{ order.id | json }} note: {{ order_note_to_save | json }} } ) { userErrors { field message } } } {% endaction %} {% endif %} {% endfor %} {% endif %}
12