Temporarily add an order note with Mechanic.

Mechanic is the one-tool-does-it-all automation app for Shopify. :)

Temporarily add an order note

by Isaac Bowen (team@usemechanic.com)

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 12 hours after user/orders/remove_note is triggered. Configuration includes order note to add.

15-day free trial – unlimited tasks

Documentation

Enter one or more order numbers (one line apiece), and 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.

Note: Mechanic will only look at authorized and paid orders from the last 60 days.

Developer details

Events
when some text is submitted (mechanic/user/text)
12 hours after user/orders/remove_note is triggered (user/orders/remove_note+12.hours)
Options
order note to add (required)
Script
{% capture newline %}
{% endcapture %}

{% if event.topic == "mechanic/user/text" %}
  {% if event.preview %}
    {
      "action": {
        "type": "shopify",
        "options": [
          "update",
          [
            "order",
            12345
          ],
          {
            "note": {{ options.order_note_to_add__required | json }}
          }
        ]
      }
    }

    {
      "action": {
        "type": "event",
        "options": {
          "topic": "user/orders/remove_note",
          "data": {
            "order_id": 12345,
            "order_note_content_to_remove": {{ options.order_note_to_add__required | json }}
          }
        }
      }
    }
  {% else %}
    {% assign order_numbers = event.data | strip | split: newline %}
    {% assign order_ids = "" %}

    {% comment %}
      going to do this twice: once for authorized orders, and once for paid orders
    {% endcomment %}

    {% for order in shop.orders.any.authorized %}
      {% capture order_number_string %}{{ order.order_number }}{% endcapture %}
      {% if order_numbers contains order_number_string %}
        {% capture order_note_to_save %}{% if order.note != blank %}{{ order.note }}{{ newline }}{{ newline }}{% endif %}{{ options.order_note_to_add__required }}{% endcapture %}

        {% capture order_ids %}{% if order_ids != blank %}{{ order_ids }},{% endif %}{{ order.id }}{% endcapture %}

        {
          "action": {
            "type": "shopify",
            "options": [
              "update",
              [
                "order",
                {{ order.id | json }}
              ],
              {
                "note": {{ order_note_to_save | json }}
              }
            ]
          }
        }
      {% endif %}
    {% endfor %}

    {% for order in shop.orders.any.paid %}
      {% capture order_number_string %}{{ order.order_number }}{% endcapture %}
      {% if order_numbers contains order_number_string %}
        {% capture order_note_to_save %}{% if order.note != blank %}{{ order.note }}{{ newline }}{{ newline }}{% endif %}{{ options.order_note_to_add__required }}{% endcapture %}

        {% capture order_ids %}{% if order_ids != blank %}{{ order_ids }},{% endif %}{{ order.id }}{% endcapture %}

        {
          "action": {
            "type": "shopify",
            "options": [
              "update",
              [
                "order",
                {{ order.id | json }}
              ],
              {
                "note": {{ order_note_to_save | json }}
              }
            ]
          }
        }
      {% endif %}
    {% endfor %}

    {% assign order_ids = order_ids | split: "," %}
    {% if order_ids.size > 0 %}
      {
        "action": {
          "type": "event",
          "options": {
            "topic": "user/orders/remove_note",
            "data": {
              "order_ids": {{ order_ids | json }},
              "order_note_content_to_remove": {{ options.order_note_to_add__required | json }}
            }
          }
        }
      }
    {% endif %}
  {% endif %}
{% elsif event.topic == "user/orders/remove_note" %}
  {% if event.preview %}
    {
      "action": {
        "type": "shopify",
        "options": [
          "update",
          [
            "order",
            12345
          ],
          {
            "note": ""
          }
        ]
      }
    }
  {% else %}
    {% for order_id in event.data.order_ids %}
      {% assign order = shop.orders[order_id] %}
      {% 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 %}

      {
        "action": {
          "type": "shopify",
          "options": [
            "update",
            [
              "order",
              {{ order.id | json }}
            ],
            {
              "note": {{ order_note_to_save | json }}
            }
          ]
        }
      }
    {% endfor %}
  {% endif %}
{% endif %}
Yep, that’s just Liquid! If this is interesting to you, check out our documentation! Mechanic is hugely developer-friendly. :)