Demonstration: Write to a customer metafield, with Mechanic.

Mechanic is a development 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 when a user triggers the task. Configuration includes customer, metafield namespace, metafield key, metafield value, use rest api, and use graphql api.

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 metafield, if any, having the desired namespace and key
  • Re-use the existing metafield's ID to perform a metafield update, if applicable, and resulting in a newly-created metafield otherwise

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 namespace and key (which will uniquely identify the metafield), and the metafield value (which will be stored within the metafield). Finally, choose between REST and GraphQL 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.

To verify that the metafield operation succeeded, use an app like Metafields Guru, or look up /metafields.json for the customer in your Shopify admin area. For example, for a customer with the ID 12345, on a store called example.myshopify.com, the following URL would show all the metafields for that customer:

https://example.myshopify.com/admin/customers/12345/metafields.json

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.

Open source
View on GitHub to contribute to this task
Events
when a user triggers the task (mechanic/user/trigger)
Options
customer (number, required), metafield namespace (required), metafield key (required), metafield value (required), use rest api (boolean), use graphql api (boolean)
Script
{% assign customer_id = options.customer_id__number_required %}
{% assign metafield_namespace = options.metafield_namespace__required %}
{% assign metafield_key = options.metafield_key__required %}
{% assign metafield_value = options.metafield_value__required %}

{% assign customer = shop.customers[customer_id] %}
{% assign existing_metafield = customer.metafields | where: "namespace", metafield_namespace | where: "key", metafield_key | first %}

{% if options.use_rest_api__boolean %}
  {% action "shopify" %}
    [
      "update",
      [
        "customer",
        {{ customer_id | json }}
      ],
      {
        "metafields": [
          {
            "id": {{ existing_metafield.id | json }},
            "namespace": {{ metafield_namespace | json }},
            "key": {{ metafield_key | json }},
            "value": {{ metafield_value | json }},
            "value_type": "string"
          }
        ]
      }
    ]
  {% endaction %}
{% elsif options.use_graphql_api__boolean %}
  {% action "shopify" %}
    mutation {
      customerUpdate(
        input: {
          id: {{ customer.admin_graphql_api_id | json }}
          metafields: [
            {
              id: {{ existing_metafield.admin_graphql_api_id | json }}
              namespace: {{ metafield_namespace | json }}
              key: {{ metafield_key | json }}
              value: {{ metafield_value | json }}
              valueType: STRING
            }
          ]
        }
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% else %}
  {% error "Choose at least one API option to continue. :)" %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Metafield namespace
foo
Metafield key
bar
Metafield value
baz