I’m using the following in function.php and it works great on a single product page. The issue I have is on the cart page when you choose a different quantity it doesn’t automatically update the cart. Any ideas?
function woocommerce_quantity_input( $args = array(), $product = null, $echo = true ) {
if ( is_null( $product ) ) {
$product = $GLOBALS['product'];
}
$defaults = array(
'input_id' => uniqid( 'quantity_' ),
'input_name' => 'quantity',
'input_value' => '1',
'classes' => apply_filters( 'woocommerce_quantity_input_classes', array( 'input-text', 'qty', 'text' ), $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', -1, $product ),
'min_value' => apply_filters( 'woocommerce_quantity_input_min', 0, $product ),
'step' => apply_filters( 'woocommerce_quantity_input_step', 1, $product ),
'pattern' => apply_filters( 'woocommerce_quantity_input_pattern', has_filter( 'woocommerce_stock_amount', 'intval' ) ? '[0-9]*' : '' ),
'inputmode' => apply_filters( 'woocommerce_quantity_input_inputmode', has_filter( 'woocommerce_stock_amount', 'intval' ) ? 'numeric' : '' ),
'product_name' => $product ? $product->get_title() : '',
);
$args = apply_filters( 'woocommerce_quantity_input_args', wp_parse_args( $args, $defaults ), $product );
// Apply sanity to min/max args - min cannot be lower than 0.
$args['min_value'] = max( $args['min_value'], 0 );
// Change 6 to max quantity
$args['max_value'] = 0 < $args['max_value'] ? $args['max_value'] : 6;
// Max cannot be lower than min if defined.
if ( '' !== $args['max_value'] && $args['max_value'] < $args['min_value'] ) {
$args['max_value'] = $args['min_value'];
}
$options = '';
for ( $count = $args['min_value']; $count <= $args['max_value']; $count = $count + $args['step'] ) {
// Cart item quantity defined?
if ( '' !== $args['input_value'] && $args['input_value'] >= 1 && $count == $args['input_value'] ) {
$selected = 'selected';
} else $selected = '';
$options .= '<option value="' . $count . '"' . $selected . '>' . $count . '</option>';
}
$string = '<div class="quantity"><span>Qty</span><select name="' . $args['input_name'] . '">' . $options . '</select></div>';
if ( $echo ) {
echo $string;
} else {
return $string;
}
}
2
Answers
Caution: First you should never overwrite WooCommerce core files, for many reasons. So it’s prohibited.
Instead as
woocommerce_quantity_input()
function call the template fileglobal/quantity-input.php
, you can override that template via your child theme.To understand how to override templates, read carefully: Overriding templates via a theme in WooCommerce.
Now, remove all your related quantity changes and code from you web site (restore everything as before).
Then copy
quantity-input.php
file located inside WooCommerce plugin > templates > global, to your child theme into a "woocommerce" folder > "global" subfolder.Once done, open / edit it, and replace the template content with:
Now some jQuery code is required, to make things work on cart page.
This code goes in functions.php file of the active child theme (or active theme).
Now to restrict the max quantity to 6, add the following code:
This code goes in functions.php file of the active child theme (or active theme).
Now it works everywhere (tested on last WooCommerce version under Storefront theme).
The default WooCommerce Add to Cart “Quantity Input” is a simple input field where you can enter the number of items or click on the “+” and “-” to increase/reduce the quantity.
To let the users choose the quantity from a drop-down instead of having to manually input the number. This can be done using woocommerce_quantity_input function. Simply add the following Snippet to your Functions.php.