Anthony Navetta
Anthony Navetta

Reputation: 1

simpleCart.js - Calling quantity of a single item in cart with beforeAdd();

I have searched the simpleCart documentation, found here, the github page, and here extensively, to find an answer to what should be a simple issue to solve:

I need to get the quantity of a single item in the cart, so I can check it against a custom attribute containing a maximum quantity, along with the quantity being sent to the cart when an item is added.

simpleCart.quantity(); gives me the total for ALL the items in the cart, same with simpleCart.item.quantity();. Meanwhile, item.get('quantity') gives me the amount I am sending to the cart, which I need but isnt the value I am looking for.

Here is the relevant code for context

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;
    }
}

I am aware this exact question exists on this site, but it recieved no response. Please can someone help me?

Upvotes: 0

Views: 1085

Answers (1)

Matthew K
Matthew K

Reputation: 993

I think you were really close. I used 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;
}
});

Upvotes: 1

Related Questions