Monitor customer note for certain information with Mechanic.

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

Monitor customer note for certain information

by Isaac Bowen (team@usemechanic.com)

Use this task to keep an eye on the contents of a customer's note. When the text you want to watch for is added (e.g. "Authorization number:", or "VAT Registration Number:", or anything else!), Mechanic will add the tag of your choice to the customer, and optionally send you a notification email.

Runs when a customer is created and when a customer is updated. Configuration includes text to watch for in the customer note, require a case sensitive match, customer tag to apply, and staff notification email recipient.

15-day free trial – unlimited tasks

Documentation

Please note: Mechanic uses the absence of this task's tag to determine if any work should be done. This means that if the tag Mechanic adds is later removed, and if the customer note still has the matching text, Mechanic will re-tag the customer (and re-send a notification email, if configured) the next time the customer is updated.

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 a customer is created (shopify/customers/create)
when a customer is updated (shopify/customers/update)
Options
text to watch for in the customer note (required), require a case sensitive match (boolean), customer tag to apply (required), staff notification email recipient (email)
Script
{% comment %}
  Express an opinion about option order:

  {{ options.text_to_watch_for_in_the_customer_note__required }}
  {{ options.require_a_case_sensitive_match__boolean }}
  {{ options.customer_tag_to_apply__required }}
  {{ options.staff_notification_email_recipient__email }}
{% endcomment %}

{% assign match_text = options.text_to_watch_for_in_the_customer_note__required | strip %}

{% assign customer_qualifies = false %}
{% assign customer_tags = customer.tags | split: ", " %}

{% assign case_sensitive = options.require_a_case_sensitive_match__boolean %}

{% unless customer_tags contains options.customer_tag_to_apply__required %}
  {% if options.require_a_case_sensitive_match__boolean %}
    {% if customer.note contains match_text %}
      {% assign customer_qualifies = true %}
    {% endif %}
  {% else %}
    {% assign customer_note_downcased = customer.note | downcase %}
    {% assign match_text_downcased = match_text | downcase %}
    {% if customer_note_downcased contains match_text_downcased %}
      {% assign customer_qualifies = true %}
    {% endif %}
  {% endif %}
{% endunless %}

{% if event.preview or customer_qualifies %}
  {% if options.staff_notification_email_recipient__email != blank %}
    {% capture email_subject %}
      [{{ shop.name }}] Customer {{ customer.email }} has {{ match_text | json }} in their note
    {% endcapture %}

    {% assign replacement = "<b>" | append: match_text | append: "</b>" %}

    {% capture email_body %}
      Hi there,

      The customer note for {{ customer.email }} was found to contain some matching text:

      <blockquote>{{ customer.note | replace: match_text, replacement | default: "(preview of customer note)" }}</blockquote>

      View this customer's details:

      https://{{ shop.domain }}/admin/customers/{{ customer.id }}

      Thanks,
      Mechanic (for {{ shop.name }})
    {% endcapture %}

    {
      "action": {
        "type": "email",
        "options": {
          "to": {{ options.staff_notification_email_recipient__email | json }},
          "subject": {{ email_subject | strip | json }},
          "body": {{ email_body | unindent | strip | newline_to_br | json }},
          "reply_to": {{ shop.customer_email | json }},
          "from_display_name": {{ shop.name | json }}
        }
      }
    }
  {% endif %}

  {
    "action": {
      "type": "shopify",
      "options": [
        "update",
        [
          "customer",
          {{ customer.id | json }}
        ],
        {
          "tags": {{ customer.tags | add_tag: options.customer_tag_to_apply__required | json }}
        }
      ]
    }
  }
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.