Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

377
Views
how to set products limit in cart shopify

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() 
                  }

});

                  })                    


              }
          },

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!