Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce Dropdown Product Quantity (not compatible with Mix and Match, Product Bundles, etc)
<?php
/**
* 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
*/
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-dropdown', 'qty' ), $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 );
$args['max_value'] = 0 < $args['max_value'] ? $args['max_value'] : 10;
// 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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment