-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlus-Minus Quantity Buttons WooCommerce Product Page & Cart page
55 lines (41 loc) · 1.57 KB
/
Plus-Minus Quantity Buttons WooCommerce Product Page & Cart page
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* @snippet Plus+ Minus- Quantity Buttons WooCommerce Product Page & Cart page
*/
// -------------
// 1. Show plus minus buttons
add_action( 'woocommerce_after_quantity_input_field', 'qortechno_display_quantity_plus' );
function qortechno_display_quantity_plus() {
echo '<button type="button" class="plus">+</button>';
}
add_action( 'woocommerce_before_quantity_input_field', 'qortechno_display_quantity_minus' );
function qortechno_display_quantity_minus() {
echo '<button type="button" class="minus">-</button>';
}
// -------------
// 2. Trigger update quantity script
add_action( 'wp_footer', 'qortechno_add_cart_quantity_plus_minus' );
function qortechno_add_cart_quantity_plus_minus() {
if ( ! is_product() && ! is_cart() ) return;
wc_enqueue_js( "
$(document).on( 'click', 'button.plus, button.minus', function() {
var qty = $( this ).parent( '.quantity' ).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 ).change();
} else {
qty.val( val + step ).change();
}
} else {
if ( min && ( min >= val ) ) {
qty.val( min ).change();
} else if ( val > 1 ) {
qty.val( val - step ).change();
}
}
});
" );
}