【问题标题】:Limit quantity of items that can be added to cart - simpleCart限制可以添加到购物车的商品数量 - simpleCart
【发布时间】:2014-12-17 15:53:00
【问题描述】:

尝试将允许添加的商品总数限制为4,此功能第一次提醒但仍将商品添加到购物车。

simpleCart.bind( 'beforeAdd' , function( item ){

        if(simpleCart.quantity() === 4 ){

             alert("You can only compare 4 items."); 
             return false;

        }

   });

【问题讨论】:

    标签: jquery simplecart


    【解决方案1】:

    SimpleCart (v3) 将一个项目传递给 beforeAdd,它仅表示要添加到购物车的项目和数量。代码需要考虑购物车中已有的所有商品。

    simpleCart.bind('beforeAdd', function (item) {
        var requestedQuantity = item.get('quantity');
        var existingItem = simpleCart.has(item);
        if (existingItem) {
            requestedQuantity += existingItem.get('quantity');
        }
        if (requestedQuantity > 4) {
            alert("You may compare at most 4 items.");
            return false;
        }
    });
    

    【讨论】:

      【解决方案2】:

      它只检查添加的第 4 个元素并不断添加元素。如果您检查数量是否等于或大于 4 以防止添加更多项目:)

      simpleCart.bind( 'beforeAdd' , function( item ){
      
          if(simpleCart.quantity() >= 4 ){
      
               alert("You can only compare 4 items."); 
               return false;
      
          }
      
      });
      

      【讨论】:

      • 嗯仍然会添加该项目。
      • 控制台有错误吗?可能有其他东西不能正常工作。
      • 没有错误,我正在使用内联 JS 添加项目,这有关系吗?
      猜你喜欢
      • 2018-02-10
      • 2020-01-14
      • 2016-12-19
      • 1970-01-01
      • 1970-01-01
      • 2014-03-23
      • 1970-01-01
      相关资源
      最近更新 更多