Skip to content

Instantly share code, notes, and snippets.

@kloon
Last active September 15, 2022 11:04
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save kloon/6495019 to your computer and use it in GitHub Desktop.
Save kloon/6495019 to your computer and use it in GitHub Desktop.
WooCommerce Dropdown Product Quantity, fully compatible with Min/Max quantities extension
<?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>';
}
?>
@yousef-rabi
Copy link

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.

@nurul1a
Copy link

nurul1a commented Jun 2, 2014

Same problem here.

@Antonio-Laguna
Copy link

I've forked and fixed the issues here: https://gist.github.com/Belelros/8eab29f48a06277a6856

@rgb4u
Copy link

rgb4u commented Dec 12, 2014

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

@Richi400
Copy link

This doesn't work. It always just adds an amount of 1 to the cart, doesn't matter which quantity you choose. :-(

any ideas?

@gadzet
Copy link

gadzet commented Jun 11, 2017

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.

@amouratoglou
Copy link

I need the default to be 0, this is not working very well.

@strarsis
Copy link

@kloon: Wouldn't it be nice to have this as a plugin?

@strarsis
Copy link

strarsis commented Mar 5, 2018

// 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;

@splozm
Copy link

splozm commented Mar 17, 2018

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 '

' . $options . '
';

}

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.

@splozm
Copy link

splozm commented Mar 18, 2018

Fixed it

@kloon

`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 '

' . $options . '
';

}`

@ScottJFreeman
Copy link

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>';
}

@besimhu
Copy link

besimhu commented Nov 28, 2018

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',

@greguly
Copy link

greguly commented Jun 4, 2019

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 :-)

@paaljoachim
Copy link

paaljoachim commented Aug 6, 2019

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?

@moh6mmad
Copy link

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

@paaljoachim
Copy link

paaljoachim commented Aug 10, 2019

@Allshookup4
Copy link

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.

@wp126
Copy link

wp126 commented Sep 15, 2022

Product Quantity Dropdown For Woocommerce that plugin also useful this provide same solution with plugin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment