Skip to content

Instantly share code, notes, and snippets.

@artikus11
Created October 9, 2023 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artikus11/ca7b7d53ebfc593ecc46b2a08793ea65 to your computer and use it in GitHub Desktop.
Save artikus11/ca7b7d53ebfc593ecc46b2a08793ea65 to your computer and use it in GitHub Desktop.
jQuery( document ).ready( function( $ ) {
$( document ).on( 'click', '.plus, .minus', function() {
// Get values
var $qty = $( this ).closest( '.quantity' ).find( '.qty' ),
currentVal = parseFloat( $qty.val() ),
max = parseFloat( $qty.attr( 'max' ) ),
min = parseFloat( $qty.attr( 'min' ) ),
step = $qty.attr( 'step' );
// Format values
if ( ! currentVal || currentVal === '' || currentVal === 'NaN' ) {
currentVal = 0;
}
if ( max === '' || max === 'NaN' ) {
max = '';
}
if ( min === '' || min === 'NaN' ) {
min = 0;
}
if ( step === 'any' || step === '' || step === undefined || parseFloat( step ) === 'NaN' ) {
step = 1;
}
// Change the value
if ( $( this ).is( '.plus' ) ) {
if ( max && (
max == currentVal || currentVal > max
) ) {
$qty.val( max );
} else {
$qty.val( currentVal + parseFloat( step ) );
}
} else {
if ( min && (
min == currentVal || currentVal < min
) ) {
$qty.val( min );
} else if ( currentVal > 0 ) {
$qty.val( currentVal - parseFloat( step ) );
}
}
// Trigger change event
$qty.trigger( 'change' );
} );
let timeout;
$( document.body).on( 'change', '.mini-cart-container input.qty', function( e ) {
let $quantityInput = $( this );
let maxQtyData = {};
let inputs = $( this ).closest( '.woocommerce-mini-cart' ).find( 'input.qty' );
inputs.each( function( index, element ) {
maxQtyData[$( element ).attr( 'name' )] = $( this ).attr( 'max' );
} );
let $cartForm = $( '#cart-panel-form' ),
$cartFormNonce = $cartForm.find( '#_wpnonce' ),
data = {};
let itemLiQty = $quantityInput.closest( 'li' );
data = {
security: $cartFormNonce.val(),
//update_cart: '1',
order_data: inputs.serialize(),
action: 'update_mini_cart',
max_data: maxQtyData
};
data[$quantityInput.attr( 'name' )] = $quantityInput.val();
data['_wpnonce'] = $cartFormNonce.val();
if ( timeout !== undefined ) {
clearTimeout( timeout );
}
timeout = setTimeout( function() {
itemLiQty.block( {
message: null,
overlayCSS: {
background: '#fff',
'z-index': 1000000,
opacity: 0.3
}
} );
$.ajax( {
type: 'POST',
url: woocommerce_params.ajax_url,
data: data,
cache: false,
success: function( response ) {
const $html = $.parseHTML( response );
$('.kadence-mini-cart-refresh').html( $html );
itemLiQty.unblock();
},
complete: function() {
itemLiQty.unblock();
}
} );
}, 1000 );
return false;
} );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment