I’ve successfully created a customer metafield: customer.metafields.c_f.proxy
Goal is allow the customer (while logged in) from the account.liquid
template, edit the value of the metafield with a form. I’m trying to use some jquery and the Metafield API to POST
the change from the user:
<form action="/admin/customers/{{ customer.id }}/metafields.json" method="POST" id="custmeta">
<input type="hidden" name="customer[id]" value="{{ customer.id }}" />
<input type="text" name="metafield[c_f.proxy]" value="{{ customer.metafields.c_f.proxy }}" placeholder="{{ customer.metafields.c_f.proxy }}" />
<input type="submit" value="Edit"/>
</form>
<script>
$('form#custmeta').submit(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
dataType: "json",
data: $(this).serialize(),
url: $(this).attr('action'),
success: function (data) {
var formValid = (data.status === 'OK');
if (formValid) {
var msgs = '';
for (var i=0;i<data.messages.length;i++) {
msgs += '-- ' + data.messages[i] + 'n';
}
if (msgs > '') {
alert('SUCCESS WITH MESSAGES:nn' + msgs);
}
else {
alert('SUCCESS!');
}
}
else {
alert('Status: ' + data.status + 'nMessage: ' + data.message);
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert('AJAX or Server 500 error occurred');
}
});
return false;
});
</script>
Currently throwing an AJAX or Server 500 error occurred.
Edit to include an answer from another forum:
what you’re doing won’t work. You’re sending data from a page to the API via JS in a browser. You’ll be sending cookies in that request. Any request that updates/adds data will be blocked if it contains cookies.
2
Answers
I've found a workaround to this solution using
customer.tags
Here's a look at my code:
Tags workaround can be useful and tricky.
A proper way would be to use an app which allow users to update metafields from the storefront, like MetaFields2 or Custom Fields.
Those apps are paying, so you can also implement it yourself easily.