Skip to content

Instantly share code, notes, and snippets.

@titodevera
Last active May 8, 2019 07:09
Show Gist options
  • Save titodevera/62dd95baa79ff6edd6ce7869cdce5f11 to your computer and use it in GitHub Desktop.
Save titodevera/62dd95baa79ff6edd6ce7869cdce5f11 to your computer and use it in GitHub Desktop.
Change WooCommerce add to cart quantity into a drop-down
<?php
/**
* Output the quantity input for add to cart forms.
*
* @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(), $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]*' : '' ),
'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'] : '';
$extra_option = false;
$def_max_val = 20;
if ( $args['max_value'] === '' || $args['max_value'] > $def_max_val ){
$args['max_value'] = $def_max_val;//default max value if not defined
if ( $args['input_value'] > $args['max_value'] ) $extra_option = $args['input_value'];
}
// Max cannot be lower than min if defined.
if ( '' !== $args['max_value'] && $args['max_value'] < $args['min_value'] ) {
$args['max_value'] = $args['min_value'];
}
if ( $args['max_value'] && $args['min_value'] === $args['max_value'] ) {
?>
<div class="quantity hidden">
<input type="hidden" id="<?php echo esc_attr( $args['input_id'] ); ?>" class="qty" name="<?php echo esc_attr( $args['input_name'] ); ?>" value="<?php echo esc_attr( $args['min_value'] ); ?>" />
</div>
<?php
} else {
/* translators: %s: Quantity. */
$label = ! empty( $args['product_name'] ) ? sprintf( __( '%s quantity', 'woocommerce' ), wp_strip_all_tags( $args['product_name'] ) ) : __( 'Quantity', 'woocommerce' );
?>
<div class="quantity">
<label class="screen-reader-text" for="<?php echo esc_attr( $args['input_id'] ); ?>"><?php echo esc_attr( $label ); ?></label>
<select name="<?php echo esc_attr( $args['input_name'] ); ?>"
id="<?php echo esc_attr( $args['input_id'] ); ?>"
class="<?php echo esc_attr( join( ' ', (array) $args['classes'] ) ); ?>"
title="<?php echo esc_attr_x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ); ?>">
<?php
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 = '';
echo '<option value="' . $count . '"' . $selected . '>' . $count . '</option>';
}
if( $extra_option != false ) echo '<option value="'.$extra_option.'" selected>'.$extra_option.'</option>';
?>
</select>
</div>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment