Add Locksmith passcodes to order notes, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Add Locksmith passcodes to order notes

by Isaac Bowen (team@usemechanic.com)

Use this task to record which Locksmith passcodes your customer used, before placing their order.

Runs when Locksmith sends a session ping and when an order is created. Configuration includes note prefix.

15-day free trial – unlimited tasks

Documentation

This task requires that Mechanic webhooks be enabled for your Shopify account. Contact Locksmith support (team@uselocksmith.com) to enable this feature. :)

Developer details

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.

Events
when Locksmith sends a session ping (locksmith/sessions/ping)
when an order is created (shopify/orders/create)
Options
note prefix
Script
{% capture newline %}
{% endcapture %}

{% if event.topic == "locksmith/sessions/ping" %}
  {% if event.preview %}
    {% assign passcode_cache_key = "order_passcodes:000000" %}
    {% assign passcode_cache_val = "letmein, members17" %}
  {% else %}
    {% capture passcode_cache_key %}order_passcodes:{{ session.cart_token }}{% endcapture %}
    {% assign passcode_cache_val = cache[passcode_cache_key] %}
  {% endif %}

  {% for param_keyval in session.params %}
    {% assign param_key = param_keyval[0] %}
    {% assign param_val = param_keyval[1] %}

    {% if param_key contains "passcode" %}
      {% comment %}
        Using add_tag for easy, guaranteed-unique, comma-separated values
      {% endcomment %}
      {% assign passcode_cache_val = passcode_cache_val | add_tag: param_val %}
    {% endif %}
  {% endfor %}

  {% if event.preview or passcode_cache_val != blank %}
    {
      "action": {
        "type": "cache",
        "options": {
          "set": {
            "key": {{ passcode_cache_key | json }},
            "value": {{ passcode_cache_val | json }}
          }
        }
      }
    }
  {% endif %}
{% elsif event.topic == "shopify/orders/create" %}
  {% if event.preview %}
    {% assign passcode_cache_val = "letmein, members17" %}
  {% else %}
    {% capture passcode_cache_key %}order_passcodes:{{ order.cart_token }}{% endcapture %}
    {% assign passcode_cache_val = cache[passcode_cache_key] %}
  {% endif %}

  {% if event.preview or passcode_cache_val != blank %}
    {
      "action": {
        "type": "shopify",
        "options": [
          "update",
          ["order", {{ order.id | json }}],
          {
            "note": {{ order.note | append: newline | append: newline | append: options.note_prefix | append: " " | append: passcode_cache_val | strip | json }}
          }
        ]
      }
    }
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Note prefix
Locksmith passcodes used: