Shopify:元字段 API -- 是否可以允许用户从帐户页面编辑元字段?

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

我已成功创建客户元字段:customer.metafields.c_f.proxy

目标是允许客户(登录时)从 account.liquid 模板中使用表单编辑元字段的值。我正在尝试使用一些 jquery 和 Metafield APIPOST 来自用户的更改:

<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>

当前抛出 AJAX 或发生服务器 500 错误。

编辑以包含来自另一个论坛的答案:

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.

我使用 customer.tags

找到了此解决方案的解决方法

看看我的代码:

{% form 'customer' %}

  {{ form.errors | default_errors }}

  {% if form.posted_successfully? %}
    <div class="form--success">
      <span>Thank you, we added that name and they can pick it up for you.</span> <!-- This might interfere with your newsletter signup, you can try remove this section and add < input type="hidden" name="redirect" value="https://www.yoursite.com/pages/success"> for redirecting to success URL -->
    </div>
  {% else %}

    <input type="hidden" id="proxyTag" name="contact[tags]" value="Proxy Pickup - ">

    {% comment %}
    <label for="proxyTag" class="label--hidden">Proxy Pickup -- </label>
    {% endcomment %}

    <input type="hidden" name="contact[email]" id="Email" class="input-group__field" placeholder="Email" value="{{ customer.email }}">

    <input type="text" id="userTag" name="contact[name]" placeholder="John Smith" required>

    <span class="input-group__btn">
      <button type="submit" name="commit" class="btn">
        <span>Save</span>
      </button>
    </span>

  {% endif %} 
{% endform %}

<script>
  $(function () {
    $('#userTag').on('change', function () {
      $('#proxyTag').val($(this).val() + ' - Proxy Pickup');
    })
  })
</script>

标记解决方法可能有用但也很棘手。 一种正确的方法是使用允许用户从店面更新元字段的应用程序,例如 MetaFields2 或自定义字段。

这些应用都是付费的,你也可以自己轻松实现。