【问题标题】:Shopify: Metafield API -- is it possible to allow users to edit metafields from the account page?Shopify:元字段 API——是否可以允许用户从帐户页面编辑元字段?
【发布时间】:2018-04-24 16:39:29
【问题描述】:

我已成功创建客户元字段: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 或 Server 500 错误。

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

你正在做的事情是行不通的。您正在通过浏览器中的 JS 将数据从页面发送到 API。您将在该请求中发送 cookie。如果包含 cookie,任何更新/添加数据的请求都将被阻止。

【问题讨论】:

    标签: javascript jquery html shopify liquid


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      我找到了使用 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>

      【讨论】:

      • 嘿@s0rfi949 您是否成功地允许用户修改标签,或者甚至有可能?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多