jQuery(function($) {
// Update quantity when plus/minus buttons are clicked
$(document).on('click', '.quantity .plus, .quantity .minus', function() {
var $quantityInput = $(this).closest('.quantity').find('input.qty');
var newQuantity = parseInt($quantityInput.val());
if ($(this).hasClass('plus')) {
newQuantity++;
} else if ($(this).hasClass('minus') && newQuantity > 1) {
newQuantity--;
}
// Get the cart item key (find the corresponding cart item in checkout)
var cartItemKey = $(this).closest('tr').attr('data-cart_item_key');
var data = {
action: 'update_checkout_quantity',
security: update_checkout_quantity.security,
cart_item_key: cartItemKey,
quantity: newQuantity
};
// Perform the AJAX request to update quantity
$.post(update_checkout_quantity.ajax_url, data, function(response) {
if (response.success) {
location.reload(); // Refresh the page to update the cart totals and items
} else {
alert(response.data.message); // Show an error if something goes wrong
}
});
});
});