Update empty customer data from addresses, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

Update empty customer data from addresses

Running when a customer is updated, this task will fill empty customer names and/or phones on accounts by pulling the relevant data from any customer addresses on file. Optionally, the task may be run in manual mode to scan and update all customers.

Runs Occurs whenever a customer is updated. Configuration includes update customer first and last name, update customer phone, and enable manual runs to scan all customers.

15-day free trial – unlimited tasks

Documentation

Running when a customer is updated, this task will fill empty customer names and/or phones on accounts by pulling the relevant data from any customer addresses on file. Optionally, the task may be run in manual mode to scan and update all customers.

This task is a combination of two separate merchant requests, the first desiring a way to backfill empty customer registration names once initial orders were made, and the second which noted that customer phone numbers were only set when they opted in to receive SMS updates during checkout.

Note: To avoid conflicts with this task running on customer updates (the default configuration), the manual run mode must be toggled by checking the "Enable manual runs to scan all customers" option AND saving the task. This will prevent the task from running on customer updates until this option is unchecked.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, 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.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
{% if options.enable_manual_runs_to_scan_all_customers__boolean %}
  mechanic/user/trigger
  mechanic/shopify/bulk_operation
{% else %}
  shopify/customers/update
{% endif %}
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
update customer first and last name (boolean), update customer phone (boolean), enable manual runs to scan all customers (boolean)
Code
{% assign update_customer_first_and_last_name = options.update_customer_first_and_last_name__boolean %}
{% assign update_customer_phone = options.update_customer_phone__boolean %}

{% unless update_customer_first_and_last_name or update_customer_phone %}
  {% error "Choose at least one of the update customer options." %}
{% endunless %}

{% if event.topic == "shopify/customers/update" %}
  {% assign customers = array %}

  {% capture query %}
    query {
      customer(id: {{ customer.admin_graphql_api_id | json }}) {
        id
        firstName
        lastName
        phone
        defaultAddress {
          firstName
          lastName
          phone
        }
        addresses {
          firstName
          lastName
          phone
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% assign customers[0] = result.data.customer %}

{% elsif event.topic == "mechanic/user/trigger" %}
  {% capture bulk_operation_query %}
    query {
      customers {
        edges {
          node {
            id
            firstName
            lastName
            phone
            defaultAddress {
              firstName
              lastName
              phone
            }
            addresses {
              firstName
              lastName
              phone
            }
          }
        }
      }
    }
  {% endcapture %}

  {% action "shopify" %}
    mutation {
      bulkOperationRunQuery(
        query: {{ bulk_operation_query | json }}
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}

{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
  {% assign customers = bulkOperation.objects %}
{% endif %}

{% if event.preview %}
  {% capture customers_json %}
    [
      {
        "id": "gid://shopify/Customer/1234567890",
        "phone": null,
        "defaultAddress": {
          "firstName": "Jean",
          "lastName": "Deaux"
        },
        "addresses": [
          {
            "phone": null
          },
          {
            "phone": "+18885559876"
          }
        ]
      }
    ]
  {% endcapture %}

  {% assign customers = customers_json | parse_json %}
{% endif %}

{% for customer in customers %}
  {% assign first_name = nil %}
  {% assign last_name = nil %}
  {% assign phone = nil %}

  {% assign customer_addresses
    = customer.addresses
    | unshift: customer.defaultAddress
  %}

  {% if update_customer_first_and_last_name %}
    {% if customer.firstName == blank %}
      {% for customer_address in customer_addresses %}
        {% unless customer_address.firstName == blank %}
          {% assign first_name = customer_address.firstName %}
          {% break %}
        {% endunless %}
      {% endfor %}
    {% endif %}

    {% if customer.lastName == blank %}
      {% for customer_address in customer_addresses %}
        {% unless customer_address.lastName == blank %}
          {% assign last_name = customer_address.lastName %}
          {% break %}
        {% endunless %}
      {% endfor %}
    {% endif %}
  {% endif %}

  {% if update_customer_phone %}
    {% if customer.phone == blank %}
      {% for customer_address in customer_addresses %}
        {% unless customer_address.phone == blank %}
          {% assign phone = customer_address.phone %}
          {% break %}
        {% endunless %}
      {% endfor %}
    {% endif %}
  {% endif %}

  {% if first_name or last_name or phone %}
    {% action "shopify" %}
      mutation {
        customerUpdate(
          input: {
            id: {{ customer.id | json }}
            {% if first_name %}
              firstName: {{ first_name | json }}
            {% endif %}
            {% if last_name %}
              lastName: {{ last_name | json }}
            {% endif %}
            {% if phone %}
              phone: {{ phone | json }}
            {% endif %}
          }
        ) {
          customer {
            id
            firstName
            lastName
            phone
          }
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endfor %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more