Skip to content

Instantly share code, notes, and snippets.

@kloon

kloon/functions.php

Last active May 14, 2020
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@yousef-rabi yousef-rabi commented May 28, 2014

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.

@amin2009

This comment has been minimized.

Copy link

@amin2009 amin2009 commented Jun 2, 2014

Same problem here.

@Antonio-Laguna

This comment has been minimized.

Copy link

@Antonio-Laguna Antonio-Laguna commented Aug 19, 2014

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

@rgb4u

This comment has been minimized.

Copy link

@rgb4u 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

This comment has been minimized.

Copy link

@Richi400 Richi400 commented Sep 11, 2015

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

This comment has been minimized.

Copy link

@gadzet 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

This comment has been minimized.

Copy link

@amouratoglou amouratoglou commented Oct 2, 2017

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

@strarsis

This comment has been minimized.

Copy link

@strarsis strarsis commented Feb 19, 2018

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

@strarsis

This comment has been minimized.

Copy link

@strarsis 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

This comment has been minimized.

Copy link

@splozm 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

This comment has been minimized.

Copy link

@splozm 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

This comment has been minimized.

Copy link

@ScottJFreeman ScottJFreeman commented Aug 23, 2018

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

This comment has been minimized.

Copy link

@besimhu 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

This comment has been minimized.

Copy link

@greguly 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

This comment has been minimized.

Copy link

@paaljoachim 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

This comment has been minimized.

Copy link

@moh6mmad moh6mmad commented Aug 10, 2019

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

This comment has been minimized.

Copy link

@paaljoachim paaljoachim commented Aug 10, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.