Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
WooCommerce - Show quantity inputs for simple products within loops.
* Code should be placed in your theme functions.php file.
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
$html .= woocommerce_quantity_input( array(), $product, false );
$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
$html .= '</form>';
return $html;
Copy link

fegul commented Jul 23, 2013

I used this override snippet but I think there is a newer version of the original template that checks if a product is purchasable. I only ask because it's caused some drastic style changes on my site. Is there a less intrusive way to link the quantity picker with the add to cart button?

Copy link

fegul commented Jul 23, 2013

Found a gist that enables AJAX with a quantity picker:
Might be useful for others since the above requires a page reload.

Copy link

webaware commented Aug 18, 2013

@fegul: I just branched this gist with something similar to the one you link (didn't know it existed!), but I also have a gist for doing this with hooks so that you can use the standard templates. Read my blog post, WooCommerce add to cart with quantity and AJAX for more details.

Copy link

ghost commented Feb 24, 2015

Is there a way to remove the functionality that makes the user jump to the top of the screen after adding an item to the cart and instead replacing it with the /loop/add-to-cart.php functionality where a message (View cart ->) appears below the button?

Copy link

kungfual commented Sep 28, 2015

Is there an update to this available for version 2.1.0 of add-to-cart

Copy link

mikejolley commented Apr 21, 2016

Updated for 2.5.

Copy link

webxpress commented Feb 26, 2017

Please i need help on adding quantity selector to my pages on wordpress but not just getting it to show up. the url is I would like the quantity selector to show before the add to cart

Copy link

fedan80 commented Jun 14, 2017

I need to have this function with ajax.

Copy link

lucyrose93 commented Aug 7, 2018

Thank you. It works great I've found, except when you select '0' in the quantity amount, it still adds one item to the basket. How would you add this to the conditional clause?

Copy link

lgdelai commented Jun 15, 2021

Hy @mikejolley

Thanks for sharing your code, I've used it for a long time.

However recently I noticed that it doesn't work with ajax. what made me look for another solution, so I found this code:

I use the ContentViews "PRO" plugin to display my products, and the quantity box does not appear on the products displayed in the plugin view.

The developers told me that:
And our plugin uses the "add_to_cart" shortcode of WooCommerce to show the "Add to cart" button.

What change can I make so that your code displays the quantities field in contentviews as well?

Or, how can I adjust your code to work with ajax?

Thank you so much

Copy link

nxmndr commented May 31, 2022

Beware, as this adds the POST to the address bar URL for some reason, meaning you lose idempotency : if you reload the page, it adds the product again.

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