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, metafield namespace, metafield key, metafield value, use rest api, use graphql api, and use metafields set mutation.

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-using the existing metafield's ID to perform a metafield update, if applicable, and resulting in a newly-created metafield otherwise
  • Using the metafieldsSet GraphQL mutation, which does not require a metafield ID [Note: this mutation is available as of the 2021-10 API release]

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, 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), metafield namespace (required), metafield key (required), metafield value (required), use rest api (boolean), use graphql api (boolean), use metafields set mutation (boolean)
Code
{% 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 }},
            "type": "single_line_text_field"
          }
        ]
      }
    ]
  {% 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 }}
              type: "single_line_text_field"
            }
          ]
        }
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}

{% elsif options.use_metafields_set_mutation__boolean %}
  {% action "shopify" %}
    mutation {
      metafieldsSet(
        metafields: [
          {
            ownerId: {{ customer.admin_graphql_api_id | json }}
            namespace: {{ metafield_namespace | json }}
            key: {{ metafield_key | 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 %}

{% else %}
  {% error "Choose at least one API option to continue. :)" %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Metafield namespace
foo
Metafield key
bar
Metafield value
baz