我搜索了simpleCart文档,发现here 、github 页面,以及此处广泛的内容,以找到应该解决的简单问题的答案:
我需要获取购物车中单个商品的数量,以便我可以根据包含最大数量的自定义属性以及添加商品时发送到购物车的数量进行检查。
simpleCart.quantity();给出购物车中所有商品的总数,与 simpleCart.item.quantity(); 相同。同时,item.get('quantity') 为我提供了发送到购物车的金额,这是我需要的,但不是我正在寻找的值。
这是上下文的相关代码
if (simpleCart.has(item)) {
var mA = item.get( 'maxamount' );
var linkQty = item.get('quantity');
totalquant = (item.quantity() + linkQty);
alert('Max amount: '+mA+'\nQty sent to cart: '+linkQty+'\nQty in cart plus qty sent to cart: '+totalquant);
if(totalquant > mA){
alert("You can not select more than "+mA+" items of this size!");
return false;
}
}
我知道这个网站上存在这个确切的问题,但没有收到任何回复。请问有人可以帮助我吗?
最佳答案
我认为你们真的很亲密。我用的是SimpleCart v3
simpleCart.bind('beforeAdd', function (item) {
var requestedQuantity = item.get('quantity');
var existingItem = simpleCart.has(item);
if (existingItem) {
requestedQuantity += existingItem.get('quantity');
}
if (requestedQuantity > item.get( 'maxamount' )) {
alert("You have reached the maximum quantity of this size.");
return false;
}
});
关于javascript - simpleCart.js - 使用 beforeAdd() 调用购物车中单个商品的数量;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27120738/