|
<?php |
|
|
|
add_action('woocommerce_before_quantity_input_field', function() { |
|
if( ! is_cart()) return; |
|
echo '<button type="button" class="minus">-</button>'; |
|
}); |
|
|
|
add_action('woocommerce_after_quantity_input_field', function() { |
|
if( ! is_cart()) return; |
|
echo '<button type="button" class="plus">+</button>'; |
|
}); |
|
|
|
add_action( 'wp_footer', function() { |
|
// Only run on the cart page |
|
if ( ! is_cart() ) return; |
|
?> |
|
<script> |
|
jQuery(document).ready(function($) { |
|
|
|
// Handle + and - buttons |
|
$('.quantity').on('click', '.plus, .minus', function() { |
|
|
|
// Get the related input inside the same quantity wrapper |
|
let $qty = $(this).closest('.quantity').find('.qty'); |
|
let currentVal = parseFloat($qty.val()); |
|
let max = parseFloat($qty.attr('max')); |
|
let min = parseFloat($qty.attr('min')); |
|
let step = parseFloat($qty.attr('step')); |
|
|
|
// Handle NaN or missing values safely |
|
if (isNaN(currentVal)) currentVal = 0; |
|
if (isNaN(max)) max = 999999; |
|
if (isNaN(min)) min = 0; |
|
if (isNaN(step) || step === 0) step = 1; |
|
|
|
// Increase / decrease logic |
|
if ($(this).hasClass('plus')) { |
|
if (currentVal + step <= max) { |
|
$qty.val((currentVal + step).toFixed(step.toString().split('.')[1]?.length || 0)); |
|
} |
|
} else { |
|
if (currentVal - step >= min) { |
|
$qty.val((currentVal - step).toFixed(step.toString().split('.')[1]?.length || 0)); |
|
} |
|
} |
|
|
|
// Trigger change event so WooCommerce updates totals |
|
$qty.trigger('change'); |
|
}); |
|
|
|
}); |
|
</script> |
|
<style> |
|
#main-content table.cart tr .quantity { |
|
display: flex; |
|
align-items: stretch; |
|
} |
|
|
|
#main-content table.cart tr .quantity button {flex: 1 !important;height: auto !important;width: 36px !important;padding: 10px 14px;} |
|
.woocommerce table.cart .quantity input.qty { |
|
|
|
padding: 3px !important; |
|
} |
|
</style> |
|
<?php |
|
}); |