Created
July 7, 2021 18:40
-
-
Save jorpdesigns/2eb99a78b03cc1f9d62cb01c6dbb269c to your computer and use it in GitHub Desktop.
Snippet to add "+" and "-" signs around quantity input on WooCommerce product page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'woocommerce_before_add_to_cart_quantity', 'display_quantity_minus' ); | |
function display_quantity_minus() { | |
echo '<div class="quantity-wrapper"><span>Qty</span><button type="button" class="minus" >-</button>'; | |
} | |
add_action( 'woocommerce_after_add_to_cart_quantity', 'display_quantity_plus' ); | |
function display_quantity_plus() { | |
echo '<button type="button" class="plus" >+</button></div>'; | |
} | |
// TRIGGER JQUERY SCRIPT | |
add_action( 'wp_footer', 'add_cart_quantity_plus_minus' ); | |
function add_cart_quantity_plus_minus() { | |
if ( ! is_product() ) return; | |
?> | |
<script type="text/javascript"> | |
jQuery(document).ready(function($){ | |
$('form.cart').on( 'click', 'button.plus, button.minus', function() { | |
var qty = $( this ).closest( 'form.cart' ).find( '.qty' ); | |
var val = parseFloat(qty.val()); | |
var max = parseFloat(qty.attr( 'max' )); | |
var min = parseFloat(qty.attr( 'min' )); | |
var step = parseFloat(qty.attr( 'step' )); | |
if ( $( this ).is( '.plus' ) ) { | |
if ( max && ( max <= val ) ) { | |
qty.val( max ); | |
} else { | |
qty.val( val + step ); | |
} | |
} else { | |
if ( min && ( min >= val ) ) { | |
qty.val( min ); | |
} else if ( val > 1 ) { | |
qty.val( val - step ); | |
} | |
} | |
$( 'form.cart .qty' ).trigger("change"); | |
}); | |
}); | |
</script> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment