-
-
Save Antonio-Laguna/8eab29f48a06277a6856 to your computer and use it in GitHub Desktop.
WooCommerce Dropdown Product Quantity, fully compatible with Min/Max quantities extension and working quantity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Place the following code in your theme's functions.php file | |
// override the quantity input with a dropdown | |
// Note that you still have to invoke this function like this: | |
/* | |
$product_quantity = woocommerce_quantity_input( array( | |
'input_name' => "cart[{$cart_item_key}][qty]", | |
'input_value' => $cart_item['quantity'], | |
'max_value' => $_product->backorders_allowed() ? '' : $_product->get_stock_quantity(), | |
'min_value' => '0' | |
), $_product, false ); | |
*/ | |
function woocommerce_quantity_input($data) { | |
global $product; | |
$defaults = array( | |
'input_name' => $data['input_name'], | |
'input_value' => $data['input_value'], | |
'max_value' => apply_filters( 'woocommerce_quantity_input_max', '', $product ), | |
'min_value' => apply_filters( 'woocommerce_quantity_input_min', '', $product ), | |
'step' => apply_filters( 'woocommerce_quantity_input_step', '1', $product ), | |
'style' => apply_filters( 'woocommerce_quantity_style', 'float:left; margin-right:10px;', $product ) | |
); | |
if ( ! empty( $defaults['min_value'] ) ) | |
$min = $defaults['min_value']; | |
else $min = 1; | |
if ( ! empty( $defaults['max_value'] ) ) | |
$max = $defaults['max_value']; | |
else $max = 20; | |
if ( ! empty( $defaults['step'] ) ) | |
$step = $defaults['step']; | |
else $step = 1; | |
$options = ''; | |
for ( $count = $min; $count <= $max; $count = $count+$step ) { | |
$selected = $count === $defaults['input_value'] ? ' selected' : ''; | |
$options .= '<option value="' . $count . '"'.$selected.'>' . $count . '</option>'; | |
} | |
echo '<div class="quantity_select" style="' . $defaults['style'] . '"><select name="' . esc_attr( $defaults['input_name'] ) . '" title="' . _x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) . '" class="qty">' . $options . '</select></div>'; | |
} | |
?> |
/**---------------------------------------------------------------------------
* Change quantity in to dropdown
----------------------------------------------------------------------------*/
function woocommerce_quantity_input($data = null) {
global $product;
if (!$data) {
$defaults = array(
'input_name' => 'quantity',
'input_value' => '1',
'max_value' => apply_filters( 'woocommerce_quantity_input_max', '', $product ),
'min_value' => apply_filters( 'woocommerce_quantity_input_min', '', $product ),
'step' => apply_filters( 'woocommerce_quantity_input_step', '1', $product ),
'style' => apply_filters( 'woocommerce_quantity_style', 'float:left;', $product )
);
} else {
$defaults = array(
'input_name' => $data['input_name'],
'input_value' => $data['input_value'],
'max_value' => apply_filters( 'woocommerce_quantity_input_max', '', $product ),
'min_value' => apply_filters( 'woocommerce_quantity_input_min', '', $product ),
'step' => apply_filters( 'woocommerce_quantity_input_step', '1', $product ),
'style' => apply_filters( 'woocommerce_quantity_style', 'float:left;', $product )
);
}
if ( ! empty( $defaults['min_value'] ) )
$min = $defaults['min_value'];
else $min = 1;
if ( ! empty( $defaults['max_value'] ) )
$max = $defaults['max_value'];
else $max = 100;
if ( ! empty( $defaults['step'] ) )
$step = $defaults['step'];
else $step = 1;
$options = '';
//fix add to cart data to send on cart page
if (!empty($defaults['input_name'])) {
$name = esc_attr($defaults['input_name']);
} else {
$name = 'quantity';
}
for ( $count = $min; $count <= $max; $count = $count+$step ) {
$selected = $count === $defaults['input_value'] ? ' selected' : '';
$options .= '<option value="' . $count . '"'.$selected.'>' . $count . '</option>';
}
echo '<div class="quantity_select" style="' . $defaults['style'] . '"><select name="' . $name . '" title="' . _x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) . '" class="qty">' . $options . '</select></div>';
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To get it working on both product pages and cart, you need to ensure the
name
attribute is set correctly.And then use this in your select element, mine is like so
<select name="' . $name . '" title="' . _x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) . '" class="qty">' . $options . '</select></div>';