【问题标题】:Shopify - remove/change quantity of multiple cart itemsShopify - 删除/更改多个购物车项目的数量
【发布时间】:2020-06-22 07:06:03
【问题描述】:

我正在尝试弄清楚如何调整多个购物车项目。

基本上,我们有一个自定义订单页面,可将多个产品添加到购物车中。添加的所有产品都具有相同的独特属性。

例如,将这两种产品添加到购物车中:

Product 1
ID: 1000
Property: CustomProduct2

Product2 
ID: 1001
Property: CustomProduct2

最终用户只将其视为一种产品,因此我想要一种方法来删除或调整所有具有匹配属性的产品的数量,只需一个按钮。

我知道下面的方法行不通,但如果可能的话,我想应该是这样的:

$(document).on('click','.remove',function(e){
  var property = $(this).attr('data-property');
       $.ajax({
         type: 'POST',
         url: '/cart/add.js',
         data: {
           quantity: 0,
           id: *,
           properties: {
             'Property': data-property
           }
         },
         dataType: 'json',
         async:false,

       });
     });

【问题讨论】:

    标签: shopify


    【解决方案1】:

    使用 fetch 移除

    从购物车中移除一件商品。

    function removeItemFromCart (item) {
      fetch('/cart/update.js', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          updates: {
            [item.key]: 0
          }
        })
      }).then(response => response.json())
        .then((newCart) => {
          console.log('Updated cart:', newCart)
        })
        .catch(console.error)
    }
    

    【讨论】:

      【解决方案2】:

      这可以通过使用/cart/update.js 端点来实现。 (见Shopify's official documentation

      文档忽略的一点是,您可以使用变体 ID 或行项目的“键”值作为有效负载的键。这在使用行项目属性时很重要,因为如果每次都使用不同的行属性多次添加相同的变体 ID,则它可能存在于多行上。但是,对于购物车中的每一行,该键都保证是唯一的。

      因此一个示例请求是:

      $.ajax({
           type: 'POST',
           url: '/cart/update.js',
           data: {
             updates:{
                "100000:abcdef":0, // Use the line-item key inside the quotes 
                "100001:xyzwnm":0
             }
           },
           dataType: 'json',
           async:false,  // Be warned, async:false has been deprecated in jQuery for a long time and is not recommended for use. It's generally recommended to use callbacks or promises instead
      
         });
      

      创建updates 数据的一种方法是通过一个简单的for 循环。假设您将购物车的当前内容保存到名为 cart 的变量中,可能如下所示:

      var updateData = {}
      for(var i=0; i < cart.items.length; i++){
        var item = cart.items[i];
        if( /* Check for item that needs to be removed */){
          updateData[item.key] = 0;
        }
      }
      // Now you can make your AJAX call using this updateData object
      

      如果你想变得漂亮,也可以使用 array.reduce 来做到这一点:

      var updateData = cart.items.reduce(function(acc, item){
        if( /* Check for item that we want to remove */){
          acc[item.key] = 0
        }
        return acc;
      }, {})
      // Now make your AJAX call using the updateData that we created
      

      不管怎样,我们最终的 AJAX 调用现在看起来像这样:

      $.ajax({
       type: 'POST',
       url: '/cart/update.js',
       data: {
         updates: updateData
       },
       dataType: 'json',
       success: function(cart){ console.log('Hooray!', cart) },
       error: function(err){ console.error('Booo!', err) }
      

      });

      希望这会有所帮助!

      【讨论】:

      • 非常感谢,这样就可以了。我唯一苦苦挣扎的是如何为行项目键使用变量。我创建了一个名为 modify 的变量,它具有正确的信息,但它没有被视为变量。数据:{更新:{修改}},
      • 更新以举例说明如何构建“更新”对象...
      • 哦!重新阅读您的评论,我认为您只需要去掉 modify 周围的花括号 - 所以只需 data: { updates: modify }
      • 完美。再次感谢你:) :) :)
      • 所以我尝试用var quantity = $(this).val(); var keys = $(this).attr('data-keys').split(','); var updateData = {}; for(var i=0; i &lt; keys.length; i++){ var item = keys[i]; updateData[item] = quantity; } console.log(updateData);创建更新数据
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-06
      • 1970-01-01
      • 2013-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多