Mechanic Get Mechanic
Task library

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.

Try this task

When it runs

Occurs whenever a customer is updated

What you choose

  • Customer data to update
  • Enable manual runs to scan all customers

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.

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

{% if options.enable_manual_runs_to_scan_all_customers__boolean %}
  mechanic/user/trigger
  mechanic/shopify/bulk_operation
{% else %}
  shopify/customers/update
{% endif %}
How task subscriptions work

Option types and flags

Task code

{% assign customer_data_to_update = options.customer_data_to_update__multiselect_o1_first_and_last_name_o2_phone_required %}

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

  {% capture query %}
    query {
      customer(id: {{ customer.admin_graphql_api_id | json }}) {
        id
        firstName
        lastName
        defaultPhoneNumber {
          phoneNumber
        }
        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
            defaultPhoneNumber {
              phoneNumber
            }
            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",
        "defaultPhoneNumber": {
          "phoneNumber": 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 customer_data_to_update contains "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 customer_data_to_update contains "phone" %}
    {% if customer.defaultPhoneNumber.phoneNumber == 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 %}
    {% log customer_before_update: customer %}
    
    {% 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
            defaultPhoneNumber {
              phoneNumber
            }
          }
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endfor %}
Mechanic Liquid reference

Default option values

Enable manual runs to scan all customers
false