Mechanic is a development and ecommerce automation platform for Shopify. :)
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.
This task demonstrates how to write to a Shopify metafield, belonging to a specific resource - in this case, a customer.
This example covers:
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
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?)
mechanic/user/trigger
{% 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 %}
foo
bar
baz