Hi Karl,
I’ve had a hard time finding information about this newish feature of metafields, and some confusing bits too, as this section of the same documentation (Customer section) seems to suggest a different approach:
Add metafield to an existing customer
PUT /admin/api/2019-04/customers/#{customer_id}.json
{
"customer": {
"id": 207119551,
"metafields": [
{
"key": "new",
"value": "newvalue",
"value_type": "string",
"namespace": "global"
}
]
}
}
I’ve not been able to get the above to go through, only 406 errors that way.
Using your snippet, I can manually type out an array that is accepted by the Shopify API.
For instance, I’m trying to save a list of emails in a customer metafield and if I paste this as the “value”, it goes through:
"[\"guy@buddy.co\", \"friend@friend.co\", \"friend@friend.com\"]"
but if I use javascript code (node v10) to join two lists of emails together (where both vars are arrays)
"value": existing_list.concat(new_invites)
nothing is updated and no error is thrown… So I wonder what to make of that.
If you’ve any thoughts or notice anything would be much appreciated, I can’t find any documentation on working with arrays as the value of a “json_string” metafield type :S
Complete snippet:
let data = await fetch(`${shopifyAPI}/customers/${customerId}/metafields.json`, {
"method": "POST",
"headers": postHeaders,
"body": JSON.stringify({
"metafield": {
"namespace": "referrals",
"key": "invited_emails",
"value": emailsArray,
"value_type": "json_string"
}
})
})
Best
Michel