Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce - Show quantity inputs for simple products within loops.
<?php
/**
* 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;
}
@fegul

This comment has been minimized.

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?

@fegul

This comment has been minimized.

fegul commented Jul 23, 2013

Found a gist that enables AJAX with a quantity picker: https://gist.github.com/claudiosmweb/5114131
Might be useful for others since the above requires a page reload.

@webaware

This comment has been minimized.

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.

@ghost

This comment has been minimized.

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?

@kungfual

This comment has been minimized.

kungfual commented Sep 28, 2015

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

@mikejolley

This comment has been minimized.

Owner

mikejolley commented Apr 21, 2016

Updated for 2.5.

@webxpress

This comment has been minimized.

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 marigoldsignatureng.com/ftd. I would like the quantity selector to show before the add to cart

@fedan80

This comment has been minimized.

fedan80 commented Jun 14, 2017

Hi,
I need to have this function with ajax.

@lucyrose93

This comment has been minimized.

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?

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