-
-
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>'; | |
} | |
?> |
Same problem here.
I've forked and fixed the issues here: https://gist.github.com/Belelros/8eab29f48a06277a6856
Hi, is it possible to add 2 number input fields in wooC. product page?
After few weeks of searching and fighting with code,i can't find a good solution for me ;/
I try many plugins, hooks, and tutorials already...
Maybe you will know how to set up my own product price based on width,height,and product name/id
This doesn't work. It always just adds an amount of 1 to the cart, doesn't matter which quantity you choose. :-(
any ideas?
There is a problem on a checkout page. When overriding default function this does not have the functionality to update the amount in the checkout page. It will always be 1.
I need the default to be 0, this is not working very well.
@kloon: Wouldn't it be nice to have this as a plugin?
// woocommerce-max-quantity plugin support
$product_max = isa_wc_get_product_max_limit( $product->get_id() );
if( empty( $defaults['max_value'] ) )
$defaults['max_value'] = $product_max;
I have come across the below code which build on the above it which adds the actual selected quantity into the quantity field, but for some reason it then errors on the single product page just above the quantity input. Throwing up an undefined index on the highlighted line
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 = 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 . '';
}
echo '
}
I tried to fix it by placing
if (isset($_POST['value']))
within the else function but it then throws up 'default' as an undefined variable line 25... I am not skilled with php but i am getting very frustrated with this. Any help would be greatly appreciated.
Fixed it
`function woocommerce_quantity_input($data = null) {
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;', $product )
);
$data = wp_parse_args( $data, $defaults );
if ( ! empty( $data['min_value'] ) )
$min = $data['min_value'];
else $min = 1;
if ( ! empty( $data['max_value'] ) )
$max = $data['max_value'];
else $max = 20;
if ( ! empty( $data['step'] ) )
$step = $data['step'];
else $step = 1;
$options = '';
for ( $count = $min; $count <= $max; $count = $count+$step ) {
$selected = $count === $data['input_value'] ? ' selected' : '';
$options .= '<option value="' . $count . '"'.$selected.'>' . $count . '';
}
echo '
}`
For anyone looking to use this code, I tried copying the one above by @splozm but something looks like it went awry with the "fencing" ( ``` ) code in markdown, and it got rid of a few key elements. After finangling with it, here's the working code w/ the proper quantity selected:
function woocommerce_quantity_input($data = null) {
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 = 10;
if ( ! empty( $defaults['step'] ) )
$step = $defaults['step'];
else $step = 1;
$options = '';
for ( $count = $min; $count <= $max; $count = $count+$step ) {
$selected = ($count === $data['input_value']) ? ' selected' : '';
$options .= '<option value="' . $count . '"'.$selected.'>' . $count . ''; }
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>';
}
There is a new breaking change with the input_name
where the index does not exist on a product detail page, but does so on the cart. When you use the above code that @ScottJFreeman added, it errors out on detail page and does not update values on the cart when changing.
Simply replace 'input_name' => 'quantity',
with and it will work on both:
'input_name' => array_key_exists('input_name', $data) ? $data['input_name'] : 'quantity',
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 :-)
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
Thanks for the nice function.
but I cant make this function work correctly because at the cart page all of quantities become one and when I change the quantity and update the cart they still one.
please your advice.