Demonstration: Write to a customer metafield, with Mechanic.

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

Demonstration: Write to a customer metafield

This task demonstrates how to write to a Shopify metafield, belonging to a specific resource - in this case, a customer.

Runs Occurs when a user manually triggers the task. Configuration includes customer id, customer metafield, metafield value, and mutation choice.

15-day free trial – unlimited tasks

Documentation

This task demonstrates how to write to a Shopify metafield, belonging to a specific resource - in this case, a customer.

This example covers:

  • Retrieving an existing customer metafield, if any, having the desired namespace and key
  • Using the customerUpdate GraphQL mutation, to either create a new metafield or update an existing one
  • Using the metafieldsSet GraphQL mutation, which does not require a metafield ID

To use this task, provide the numeric ID of a customer in your store. (Need help finding the customer ID?) Then, add in the desired metafield in the format of "namespace.key" (which will uniquely identify the metafield), and the metafield value (which will be stored within the metafield). Finally, choose between using the "Customer update" and "Metafields set" GraphQL mutations for performing the Shopify API operation, noting how the task preview changes to reflect your choice.

Run the task to see the metafield create/update in action. After running the mutation, the task will log out a url to the customer's unstructured metafields list, which is where the metafield should be found unless it has a metafield definition (i.e. you are testing with an existing metafield).

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
mechanic/user/trigger
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
customer id (number, required) , customer metafield (required) , metafield value (required) , mutation choice (choice, o1, customer, update, o2, metafields, set, required)
Code
{% assign customer_id = options.customer_id__number_required %}
{% assign customer_metafield = options.customer_metafield__required %}
{% assign metafield_value = options.metafield_value__required %}
{% assign mutation_choice = options.mutation_choice__choice_o1_customer_update_o2_metafields_set_required %}

{% comment %}
  -- get customer metafield data
{% endcomment %}

{% capture query %}
  query {
    customer(id: {{ customer_id | prepend: "gid://shopify/Customer/" | json }}) {
      id
      metafield(key: {{ customer_metafield | json }}) {
        id
        value
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "customer": {
          "id": "gid://shopify/Customer/1234567890",
          "metafield": {
            "id": "gid://shopify/Metafield/1234567890",
            "value": "lorem"
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = result_json | parse_json %}
{% endif %}

{% assign customer = result.data.customer %}
{% assign existing_metafield = customer.metafield.value %}

{% if customer == blank %}
  {% error %}
    "Customer not found with customer ID ({{ customer_id }})."
  {% enderror %}

  {% break %}
{% endif %}

{% unless event.preview %}
  {% log customer: customer %}
{% endunless %}

{% comment %}
  -- update customer metafield using configured mutation choice
{% endcomment %}

{% if mutation_choice == "customer_update" %}
  {% action "shopify" %}
    mutation {
      customerUpdate(
        input: {
          id: {{ customer.id | json }}
          metafields: [
            {
              id: {{ existing_metafield.id | json }}
              namespace: {{ customer_metafield | split: "." | first | json }}
              key: {{ customer_metafield | split: "." | last | json }}
              value: {{ metafield_value | json }}
              type: "single_line_text_field"
            }
          ]
        }
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}

{% elsif mutation_choice == "metafields_set" %}
  {% action "shopify" %}
    mutation {
      metafieldsSet(
        metafields: [
          {
            ownerId: {{ customer.id | json }}
            namespace: {{ customer_metafield | split: "." | first | json }}
            key: {{ customer_metafield | split: "." | last | json }}
            value: {{ metafield_value | json }}
            type: "single_line_text_field"
          }
        ]
      ) {
        metafields {
          id
          namespace
          key
          type
          value
          owner {
            ... on Customer {
              id
            }
          }
        }
        userErrors {
          code
          field
          message
        }
      }
    }
  {% endaction %}
{% endif %}

{%- capture log_message -%}
  Assuming a metafield definition has not been previously created for the metafield configured in this task, then you can find the 'unstructured' metafield using the following url: {{ shop.admin_url }}customers/{{ customer_id }}/metafields/unstructured
{%- endcapture -%}

{% log log_message %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Customer metafield
foo.bar
Metafield value
baz