Here is my code what I want is to set total cart limit 10 if user try to add more than 10 item in cart cart will automatically update and it will remove additional items for example if there is 10 products in cart and user try to add 5 more than existing 5 items will be deleted if there is 11 then 1 item will be deleted I tried my best to achieve my requirement but I think I am missing something it’s not working properly
$(".addtocart_custom").click(function(evt){
jQuery.getJSON('/cart.js', function (cart)
{
var items_new = cart.items;
var count = cart.item_count;
var item_to_remove = count - 10 ;
if (count >= 10)
{
var item_to_remove = count - 10;
if(item_to_remove > 0) {
for(var i=0;i<=items_new.length;i++)
{
var c_id = cart.items[i].id;
var c_quantity= cart.items[i].quantity;
if(c_quantity >= item_to_remove) {
var data = { 'id': c_id , 'quantity':c_quantity - item_to_remove }
debugger;
$.ajax({
type: 'POST',
url: '/cart/change.js',
data: data,
dataType: 'json',
success: {
}
});
}else{
item_to_remove = item_to_remove - c_quantity;
var data = { 'id': c_id , 'quantity':c_quantity - item_to_remove }
debugger;
$.ajax({
type: 'POST',
url: '/cart/change.js',
data: data,
dataType: 'json',
success: {
}
});
}
}
}
}
});
});