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
Its working perfectly fine when console is open but its not working when console is close
{
key: "_onProductAdded",
value: function(e) {
var t = this;
this.itemCount += e.detail.quantity,this._rerenderCart().then(function() {
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++)
{
if (count > 10)
{
var c_id = cart.items[i].id;
var c_quantity= cart.items[i].quantity;
if(c_quantity >= item_to_remove) {
var data_multiple = { 'id': c_id , 'quantity':c_quantity - item_to_remove }
debugger;
$.ajax({
type: 'POST',
url: '/cart/change.js',
data: data_multiple,
dataType: 'json',
success: {
}
});
count= count - item_to_remove;
}else{
var data_single = { 'id': c_id , 'quantity':c_quantity - 1 }
debugger;
$.ajax({
type: 'POST',
url: '/cart/change.js',
data: data_single,
dataType: 'json',
success: {
}
});
count= count - 1;
}
}
}
}
t._rerenderCart();
t.sidebarDrawer.open()
}
});
})
}
},