【问题标题】:cart data not update in shopify using ajax购物车数据未在 shopify 中使用 ajax 更新
【发布时间】:2019-05-01 14:36:21
【问题描述】:

我在 Shopify 中遇到问题。

我想使用 ajax 更新按钮点击时的购物车数量,但它会给出类似的错误

{"status":404,"message":"购物车错误","description":"找不到变体"}

这是我的 ajax 代码,

 $('.adjust-plus').click(function(){
    var qty = $(this).parent('.button-wrapper').siblings('.input-wrapper').children('.quantity').val();
    var varient = $(this).parent('.button-wrapper').siblings('.input-wrapper').children('.quantity').attr('data-id');

    jQuery.ajax({
      type: 'POST',
      async: false,
      url: '/cart/update.js',
      data: { updates: { varient : qty } },
      dataType: 'json',
      success: function() { location.href = '/cart'; }
   });
 });

目前两个变量的值都来了,所以值没有任何错误。

但是当 id 添加如下代码:

$('.adjust-plus').click(function(){
    var qty = $(this).parent('.button-wrapper').siblings('.input-wrapper').children('.quantity').val();
    var varient = $(this).parent('.button-wrapper').siblings('.input-wrapper').children('.quantity').attr('data-id');

    jQuery.ajax({
      type: 'POST',
      async: false,
      url: '/cart/update.js',
      data: { updates: { 15082896588867 : 2 } },
      dataType: 'json',
      success: function() { location.href = '/cart'; }
   });
 });

然后购物车更新成功。

【问题讨论】:

  • 将帖子发送到 js 文件没有意义。

标签: javascript jquery shopify shopify-template


【解决方案1】:

首先,删除async: false,因为这是一种非常糟糕的做法,并且在这里不需要,因为您正在正确使用success 回调。

问题本身是因为您不能将变量用作具有您正在使用的语法的对象的键。为了解决这个问题,您可以使用括号表示法,如下所示:

$('.adjust-plus').click(function() {
  var $quantity = $(this).parent('.button-wrapper').siblings('.input-wrapper').children('.quantity');
  var qty = $quantity.val();
  var varient = $quantity.data('id');

  var data = { updates: {} };
  data.updates[varient] = qty;

  jQuery.ajax({
    type: 'POST',
    url: '/cart/update.js',
    data: data,
    dataType: 'json',
    success: function() { 
      location.href = '/cart'; 
    }
  });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    相关资源
    最近更新 更多