繁体   English   中英

Shopify /cart/update.js 未按预期工作

[英]Shopify /cart/update.js not working as expected

我附上了代码,请检查:如果我将数据传递给硬编码的 AJAX 函数,例如“{更新:{40066070315190: 0, 40067491528886: 0, 40094075945142: 0} }”,那么它工作正常,但是当我尝试使它是动态的,并通过循环设置变体 ID 和数量,然后它不会更新数据,但仍会进入成功函数。

大家猜猜是什么问题?

  var formData = { updates: {} };
  jQuery.getJSON('/cart.js', function(thisCart, textStatus) { // Get cart items
    $.each(thisCart.items, function(cartIndex, cartElement) {
      if(~cartElement.title.indexOf('Build your own box')) {
        formData.updates[parseInt(cartElement.variant_id)] = 0;
      }
    });
  });
  jQuery.ajax({
    type: 'POST',
    url: '/cart/update.js',
    //         data: { updates: {40066070315190: 0, 40067491528886: 0, 40094075945142: 0} },
    data: formData,
    dataType: 'json',
    success: function(data) {
      console.log('success', data);
    },
    error: function (request, status, error) {
      alert(request.responseText);
    }
  });

还附上参考文献的 console.log 输出点击查看 CONSOLE.LOG 输出

请检查,在调用/cart/update.js API 之前,您正在获取 formData 中的数据。 当您调用两个 API 时,您需要等到第一个 API 进程并提供 formData。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM