/** Initial code from: https://gist.github.com/kloon/6495019
As the code did not fully work I received help from Helga the Viking
with this gist: https://gist.github.com/helgatheviking/ff8792fbb12f5c5367c816b8a46c70ad
* Change the quantity input to select.
* @param array $args Args for the input.
* @param WC_Product|null $product Product.
* @param boolean $echo Whether to return or echo|string.
* @return string
*/
/* My custom version. Minimum quantity of 10. Max of 500. */
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', 15, $product ),
'min_value' => apply_filters( 'woocommerce_quantity_input_min', 1, $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'], 1 );
$args['max_value'] = 1 < $args['max_value'] ? $args['max_value'] : 15;
// Max cannot be lower than min if defined.
if ( '' !== $args['max_value'] && $args['max_value'] < $args['min_value'] ) {
$args['max_value'] = $args['min_value'];
}
ob_start();
if ( $args['max_value'] && $args['min_value'] === $args['max_value'] ) {
echo '<div class="quantity hidden">
<input type="hidden" id="'. esc_attr( $args['input_id'] ) .'" class="qty" name="'. esc_attr( $args['input_name'] ) .'" value="'. esc_attr( $min_value ) .'" />
</div>';
} else {
/* translators: %s: Quantity. */
$label = ! empty( $args['product_name'] ) ? sprintf( esc_html__( '%s quantity', 'woocommerce' ), wp_strip_all_tags( $args['product_name'] ) ) : esc_html__( 'Quantity', 'woocommerce' );
echo '<div class="quantity">';
do_action( 'woocommerce_before_quantity_input_field' );
echo '<label class="screen-reader-text" for="'. esc_attr( $args['input_id'] ) .'">'. esc_attr( $label ) .'</label>';
$options = '';
for ( $count = $args['min_value']; $count <= $args['max_value']; $count = $count + $args['step'] ) {
$options .= '<option value="' . $count . '" '. selected( $args['input_value'], $count, false ) . '>' . $count . '</option>';
}
echo '<div class="quantity_select"><select name="' . esc_attr( $args['input_name'] ) . '" title="' . _x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) . '" class="qty">' . $options . '</select></div>';
do_action( 'woocommerce_after_quantity_input_field' );
echo '</div>';
}
if ( $echo ) {
echo ob_get_clean(); // WPCS: XSS ok.
} else {
return ob_get_clean();
}
}
/* Clicking "Add to Cart" in the Shop page added 1 quantity. As I have a minimum of 10 I had to add the following
code from Helga for it to work correctly. The following will add quantity of 10 to the cart. */
function kia_min_quantity( $args, $product ) {
$args['quantity'] = 1;
return $args;
}
add_filter( 'woocommerce_loop_add_to_cart_args', 'kia_min_quantity', 10, 2 );
// add_filter( 'woocommerce_quantity_input_min', 'kia_min_quantity', 10, 2 );
// Add a default value to all product page quantity dropdowns. Makes a check first to see that it is ! not in the cart.
function kia_default_quantity( $args, $product ) {
if( ! is_cart() ) {
$args['input_value'] = 30;
}
return $args;
}
add_filter( 'woocommerce_quantity_input_args', 'kia_default_quantity', 10, 2 );
// OR use this code: Add a different starting value to multiple product page quantity drop downs.
// Makes a check first to see that it is ! not in the cart.
function kia_default_quantity( $args, $product ) {
if( ! is_cart() ) {
if( 613 == $product->get_id() ) { // Condition: Only apply these arguments to Product ID of #613 Product 1.
$args['input_value'] = 70;
}
elseif ( 866 == $product->get_id() ) { // Condition: Only apply these arguments to Product ID of #866 Product 2.
$args['input_value'] = 60;
}
elseif ( 145 == $product->get_id() ) { // Condition: Only apply these arguments to Product ID of #145 Product 3.
$args['input_value'] = 50;
}
else $args['input_value'] = 30; // Condition: All other product ID's begin with the starting value of 30.
}
return $args;
}
add_filter( 'woocommerce_quantity_input_args', 'kia_default_quantity', 10, 2 );