-
-
Save kloon/6495019 to your computer and use it in GitHub Desktop.
<?php | |
// Place the following code in your theme's functions.php file | |
// override the quantity input with a dropdown | |
function woocommerce_quantity_input() { | |
global $product; | |
$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; 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 ) { | |
$options .= '<option value="' . $count . '">' . $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>'; | |
} | |
?> |
I tried out the gist and it works almost great!
It works really well on the Single Product and Cart pages.
Clicking Add to Cart from the Shop page adds the correct quantity but does not add the correct total.
How do we get the correct total?
for ( $count = $min; $count <= $max; $count = $count+$step ) {
$selected = $count === $defaults['input_value'] ? ' selected' : '';
$options .= '<option value="' . $count . '"'.$selected.'>' . $count . '';
}
Thank you this was what I was looking for
I have received much help from Helga the Viking. The result is here. https://gist.github.com/paaljoachim/e8958369a67ca3cdf509428bd1719012
Here is a tutorial: https://easywebdesigntutorials.com/woocommerce-minimum-quantity-drop-down/
First I want to thank you paaljoachim for your hard work on this code. I did fine one issue and I was hoping that maybe someone would know how to fix it.
In Woocommerce, Product / Inventory tab if you have selected "Enable this to only allow one of this item to be bought in a single order" it removes the quantity selector box by default as only 1 can be purchased. The issues is the code above causes the quantity selector box to reappear on products that have "Enable this to only allow one of this item to be bought in a single order" selected.
Product Quantity Dropdown For Woocommerce that plugin also useful this provide same solution with plugin
As of WooCommerce 3.6.4 this code is broken at the cart page.
Trouble is that $product doesn't exist when at cart page.
I have found a solution here https://pluginterritory.com/shop/quantity-dropdown-selector-for-woocommerce/
As a bonus, it also works for variable products :-)