Save Locksmith passcodes to the customer's notes with Mechanic.

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

Save Locksmith passcodes to the customer's notes

by Isaac Bowen (team@usemechanic.com)

Keeps a cached list of all the passcodes associated with a visitor, and upon sign-in or checkout, saves that list to the customer's "note" field in Shopify. (Requires activation of Locksmith webhooks - hit up team@uselocksmith.com to get started with those.)

Runs when Locksmith sends a session ping and when an order is created.

15-day free trial – unlimited tasks

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)
Script
{% if event.preview %}
  {% if event.topic == "locksmith/sessions/ping" %}
    {
      "action": {
        "type": "cache",
        "options": {
          "set": {
            "key": "passcodes_by_cart_token:d9fd589a2c70f68b0a708ab3017219eb",
            "value": "passcode (lock id)"
          }
        }
      }
    }
  {% elsif event.topic == "shopify/orders/create" %}
    {
      "action": {
        "type": "shopify",
        "options": [
          "post",
          "/admin/customers/12345.json",
          {
            "customer": {
              "note": "passcode (lock id)"
            }
          }
        ]
      }
    }

    {
      "action": {
        "type": "cache",
        "options": {
          "del": {
            "key": "passcodes_by_cart_token:d9fd589a2c70f68b0a708ab3017219eb"
          }
        }
      }
    }
  {% endif %}
{% else %}
  {% if event.topic == "locksmith/sessions/ping" and session.params.passcode %}
    {% capture cache_key %}passcodes_by_cart_token:{{ session.cart_token }}{% endcapture %}
    {% assign cache_value = cache[cache_key] %}

    {% for key in session.keys %}
      {% for condition in key.conditions %}
        {% unless condition.type == "passcode" or condition.type == "passcodes" %}
          {% break %}
        {% endunless %}

        {% capture param_key %}passcode({{ condition._id }}){% endcapture %}

        {% if session.params[param_key] != blank %}
          {% capture cache_value %}{% if cache_value != blank %}{{ cache_value }}, {% endif %}{{ session.params[param_key] }} (lock {{ key.lock._id }}){% endcapture %}
        {% endif %}
      {% endfor %}
    {% endfor %}

    {% if session.customer %}
      {% assign existing_customer_note = shop.customers[session.customer.id].note %}
      {% capture customer_note %}{% if existing_customer_note != blank %}{{ existing_customer_note }}, {% endif %}{{ cache_value }}{% endcapture %}
      {
        "action": {
          "type": "shopify",
          "options": [
            "put",
            "/admin/customers/{{ session.customer.id }}.json",
            {
              "customer": {
                "note": {{ customer_note | json }}
              }
            }
          ]
        }
      }

      {
        "action": {
          "type": "cache",
          "options": {
            "del": {
              "key": {{ cache_key | json }}
            }
          }
        }
      }
    {% else %}
      {
        "action": {
          "type": "cache",
          "options": {
            "set": {
              "key": {{ cache_key | json }},
              "value": {{ cache_value | json }}
            }
          }
        }
      }
    {% endif %}
  {% elsif event.topic == "shopify/orders/create" and order.customer and order.cart_token %}
    {% capture cache_key %}passcodes_by_cart_token:{{ order.cart_token }}{% endcapture %}
    {% assign cache_value = cache[cache_key] %}

    {% if cache_value != blank %}
      {% assign existing_customer_note = shop.customers[order.customer.id].note %}
      {% capture customer_note %}{% if existing_customer_note != blank %}{{ existing_customer_note }}, {% endif %}{{ cache_value }}{% endcapture %}

      {
        "action": {
          "type": "shopify",
          "options": [
            "put",
            "/admin/customers/{{ order.customer.id }}.json",
            {
              "customer": {
                "note": {{ customer_note | json }}
              }
            }
          ]
        }
      }

      {
        "action": {
          "type": "cache",
          "options": {
            "del": {
              "key": {{ cache_key | json }}
            }
          }
        }
      }
    {% endif %}
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.