This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

📣 Join the live AMA session with Adam Silverstein on open source and WordPress core! Register Now →

How to Change the WooCommerce Default Quantity Select Box

Updated on May 24, 2021

3 Min Read

There is a WooCommerce default quantity input box in the product pages. However, there is not much you could do with this box.

A common use case is “idiot proofing” this box to make sure that the users could not enter their own values in the box. Instead, they could only select from the presented options. Note that this box is a text field.

This is how the default WooCommerce product page quantity box looks like:


By default,  WooCommerce cart page shows the quantity box as a range:

I will now demonstrate how to replace quantity text fields with a dropdown box. This will be done through the following code (located in functions.php of the theme folder):

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'],
'step'         => apply_filters( 'cw_woocommerce_quantity_input_step', '1', $product ),

'max_value'     => apply_filters( 'cw_woocommerce_quantity_input_max', '', $product ),

'min_value'     => apply_filters( 'cw_woocommerce_quantity_input_min', '', $product ),

'style'         => apply_filters( 'cw_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 = 15;



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="cw_quantity_select" style="' . $defaults['style'] . '"><select name="' . esc_attr( $defaults['input_name'] ) . '" title="' . _x( 'Qty', 'Product Description', 'woocommerce' ) . '" class="cw_qty">' . $options . '</select></div>';



}

 

 

Once the code has been added, the WooCommerce default quantity box will change to the dropbox:

The cart page will look like:

WooCommerce Stores Need a Reliable Hosting Stack

See how Cloudways helped Silicon Dales to manage their hosting stack

Final Thoughts

In this tutorial, I discussed how to display quantity select box as a dropdown, instead of the WooCommerce default quantity input option. If you need help with the code or would like to suggest improvements, do leave a comment below.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Owais Alam

is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at [email protected]

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now