I want to do an AJAX update (a hidden update cart button click) of my WooCommerce cart when user changes the quantity.
I followed the steps here.
So basically it’s this code:
jQuery(document).ready(function($) {
let timeout;
$('.woocommerce').on( 'change', 'input.qty', function(){
if ( timeout !== undefined ) {
clearTimeout( timeout );
}
timeout = setTimeout(function() {
$("[name='update_cart']").trigger("click"); // trigger cart update
}, 1000 ); // 1 second delay, half a second (500) seems comfortable too
});
});
I’ve added the above script like so:
function custom_enqueue_cart_scripts() {
error_log('custom_enqueue_cart_scripts');
if (is_cart()) {
error_log('is_cart()');
wp_enqueue_script('custom-cart', get_stylesheet_directory_uri() . '/js/custom-cart.js', array('jquery'), null, true);
}
}
add_action('wp_enqueue_scripts', 'custom_enqueue_cart_scripts');
However, it only works when I change the quantity for the first time. Any subsequent click is not registered. Why?