Shopify: Metafield API -- is it possible to allow users to edit metafields from the account page?

s0rfi949

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:\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 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.

Pol Conin

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Shopify - Update/Delete metafield via metafield API using PHP

Is it possible to allow many users to make live broadcasts using YouTube API and a single account?

Shopify Liquid / API: Accessing another customers' metafield data in liquid

Can you setup a Shopify metafield for all products in one API call?

Allow users to edit their account without providing a password | Rails4

Shopify Liquid - Can You Get a Specific Page's Metafields On A Product Page?

Loading Shopify Product Metafields in snippets

{{ product.metafields.myfields.myfield.updated_at }} | Shopify Liquid | Metafields

Restrict users from updating certain fields but allow backend to edit them

is it possible to send discount code from cart page to checkout page in shopify?

Devise: Allow users to edit their account without providing a password BUT also use 'reconfirmable' functionality

Is it possible to edit a HTML page from a different file?

Access JSON lumps in metafields with Shopify Liquid templates

How to create a shopify metafields editor app?

Keycloak: Update custom user attribute from "Edit Account"-page

Shopify Looping Over JSON Metafield Arrays

Shopify: how to retrieve shop level metafield in the cartpage?

after logout from users account all page shows error

Shopify: how to access a Shopify metafield using JavaScript in a .liquid file?

Enabling Users to Edit Custom Account Settings in MVC

Allow only authenticated users on the page

Trying to allow users to edit own articles -- Django

Allow users to create files but not edit and delete them

Meteor - Allow multiple users to edit a post

Admin user allow to delete and edit other users

Can I update shopify customer metafields now through theme file?

Is there any way to add metafields data on the homepage of shopify site?

Is there a way to allow users to run Azure DevOps pipelines, but not allow them to edit?

How to edit different shopify storefront with one script from Shopify app