Allowing customers to edit a custom metafield from the account.liquid page

Shopify Partner
110 0 6

I've successfully created a customer metafield: customer.metafields.c_f.proxy_pickup

Goal is allow the customer (logged in) and on the account.liquid template, be able to edit the value of the metafield. I'm trying to use some jquery and the Metafield API to POST this 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_pickup]" value="{{ customer.metafields.c_f.proxy_pickup }}" placeholder="{{ customer.metafields.c_f.proxy_pickup }}" />
    <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:\n\n' + 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 keep getting the Alert of AJAX or Server 500 error occurred

Is this even possible?

0 Likes
Highlighted
Shopify Partner
28 0 1

Requires admin rights - suggest you use nodejs

The end point requires admin - gives "Authentication required" if you are not admin.

 to prove: 

use

https://yourstore.myshopify.com/admin/customers/1234567.json

 Replace "yourstore" with a valid store. 

 open it in your browser. 

{"errors":"Not Found"}  When logged in as admin. 
Authentication required  When logged in as customer.

 

Docs for Admin API->Customer

 "/admin/customers/{{ customer.id }}/metafields.json"
"/admin/customers/{{ customer.id }}.json"

 

The above endpoints are part of admin api see https://help.shopify.com/api/reference/customer

 

nodejs, API & Heroku

 It will work via the api

 I used Heroku and the API to do this with nodejs. This  tutorial: https://help.shopify.com/api/tutorials/building-node-app takes maybe 2 hours, gives

 you a lot of power. A lot better than using liquid, metafields and js. The work load is about the same.

Eddy

0 Likes