Mechanic Get Mechanic
Task library

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.

Try this task

When it runs

Occurs when a user manually triggers the task

What you choose

  • Customer ID
  • Metafield namespace
  • Metafield key
Show 4 more
  • Metafield value
  • Use rest API
  • Use GraphQL API
  • Use metafields set mutation

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

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

mechanic/user/trigger
How task subscriptions work

Option types and flags

Task 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 %}
Mechanic Liquid reference

Default option values

Metafield namespace
foo
Metafield key
bar
Metafield value
baz
Use rest API
false
Use GraphQL API
false
Use metafields set mutation
false