$(document).ready(function () { $(document).on('click', 'button.sub, button.add', function () { let input = $(this).parent().find('input.qty'); let articul = $(this).parent().parent().parent().find('.name .articul').html(); if ($(this).hasClass('sub') && input.val() > 1) input.val(parseInt(input.val()) - 1); else if ($(this).hasClass('add')) input.val(parseInt(input.val()) + 1); if (input.val() === '' || input.val() === '0' || input.val() < 0) input.addClass('warning'); else if (input.hasClass('warning')) input.removeClass('warning'); if (articul) { $.ajax({ url: '/ajax/cart_update.php', type: 'post', data: { action: 'addItem', articul: articul, quantity: input.val() }, success: function (data) { updateCart(); } }); } }).on('click', '.qty.del', function () { if (!confirm('Are you sure?')) return false; let parent = $(this).parent().parent().parent(); let articul = parent.find('.name .articul').html(); $.ajax({ url: '/ajax/cart_update.php', type: 'post', data: { action: 'deleteItem', articul: articul }, success: function (data) { parent.remove(); updateCart(); } }); updateCart(); }).on('submit', '#add_to_cart_form', function (event) { event.preventDefault(); let input = $(this).parent().find('input.qty'); if (input.val() === '' || input.val() === '0' || input.val() < 0) { input.addClass('warning'); } else { $(this).find('button').attr('disabled', true); if (input.hasClass('warning')) input.removeClass('warning'); $.ajax({ url: '/ajax/cart_update.php', type: 'post', data: { action: 'addItem', articul: $('span.articul').html(), quantity: input.val() }, success: function (data) { $('.add_to_cart_form .wrapper').html('' + '

Item has been added to the shopping cart.

' + '
\n' + ' 
' ); } }); updateCart(); } }).on('click', '.cart_close', function () { $.nmTop().close(); updateCart(); }).on('change, keyup', 'input.qty', function() { let input = $(this); if (input.val() === '' || input.val() === '0' || input.val() < 0) input.addClass('warning'); else input.removeClass('warning'); }); function updateCart() { // console.log('console : shopping cart updated'); $.ajax({ url: '/ajax/cart_info.php', type: 'POST', success: function (data) { let s = ''; if (data > 1) s = 's'; if (data === 'null' || data === '0') $('.shopping_cart_info').html('No products in request'); else $('.shopping_cart_info').html('Products in request ' + data + ' item' + s + ''); } }); } updateCart(); });