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 taskWhen it runs
Occurs when a user manually triggers the task
What you choose
- Customer ID
- Customer metafield
- Metafield value
- Mutation choice
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).
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
- Customer ID (number, required)
- Customer metafield (required)
- Metafield value (required)
- Mutation choice (choice, o1, customer, update, o2, metafields, set, required)
Task 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 %}
Mechanic Liquid reference
Default option values
- Customer metafield
foo.bar- Metafield value
baz