M
M
MaikMain2017-07-10 15:26:15
JavaScript
MaikMain, 2017-07-10 15:26:15

Issue with selected items via checkbox?

Good afternoon, the problem is this. In my product card, several checkboxes are displayed that are responsible for adding the product of the desired size and price to the cart.
52f24ecd803e4ed3a08b9a3c984c4f7d.png
If you select several variations of a product at once, then only one product is added to the session. But if, after pressing the button, add to the basket, press it again, then all the selected products will be added at once (as it should be initially), and not just one as with the first click.
Script for adding a product in JS:

$('#add_to_cart').on('click', function(e){
        e.preventDefault();
        var error = 0;
        var id_prod = $(this).data('id');
        $('.chek-list li input[type=checkbox]').each(function(i,e) {
            if($(e).is(':checked')) {
                var offer = $(this).data('offer');
                var qty = $('#offer_qty_'+ offer).val();
                if(qty !== 0) {
                  $.ajax({
                      url: '/cart/newadd',
                      data: {id: id_prod, offer: offer, qty: qty},
                      	type: 'GET',
                    success: function(res){
                        if(!res) alert('Количество не обновлено!');
                    },
                    error: function(){}
      			});
                } else {
                    showMod('Выберите количество!');
                };
            }
        });
        if(error == 0){
            showMod('Выберите размер!');    
        } else {   
        	showMod(' Товар добавлен в корзину');
        }
    });

There is a mistake somewhere here, can someone tell me what exactly?
Thank you very much in advance)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question