Add quantity fields to WooCommerce store page

How to Add Quantity Inputs to the WooCommerce Shop Page

Looking for an easy way to add a quantity input next to your add to cart button on your WooCommerce store page? Look no further - here's an easy guide.

In this article, you are going to see the step-by-step process (and code snippets) on how to add a quantity input next to your add to cart buttons on your WooCommerce store pages.

Install & activate the Code Snippets WordPress Plugin

Code Snippets WordPress Plugin

The Code Snippets WordPress plugin is a super simple way to run code on your WordPress website. With this plugin, you will no longer need to add custom code snippets to your theme’s functions.php file, which makes it way easier to edit your website’s functionality.

A snippet is a small chunk of code that you can use to add functionality to your WordPress website. The Code Snippets plugin gives you an easy-to-use interface to add snippets, run them on your website, and manage all your snippets easily.

Create a new code snippet called “Quantity Picker”

Once you have the Code Snippets WordPress plugin installed and activated, open the plugin from the left-side menu, and add a new snippet.

Add new code snippet in the Code Snippet WordPress plugin

Once you have the new code snippet created, we are going to add our customer PHP code.

Copy & paste this PHP code into the snippet

Copy this code to your computer’s clipboard and paste it into your snippet.

/**
 * Add quantity field on the archive page.
 */
function custom_quantity_field_archive() {

	$product = wc_get_product( get_the_ID() );

	if ( ! $product->is_sold_individually() && 'variable' != $product->product_type && $product->is_purchasable() ) {
		woocommerce_quantity_input( array( 'min_value' => 1, 'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity() ) );
	}

}

add_action( 'woocommerce_after_shop_loop_item', 'custom_quantity_field_archive', 15, 9 );


function custom_add_to_cart_quantity_handler() {
wc_enqueue_js( '
jQuery( "body" ).on( "click", ".quantity input", function() {
return false;
});
jQuery( "body" ).on( "change input", ".quantity .qty", function() {
var add_to_cart_button = jQuery( this ).parents( ".product" ).find( ".add_to_cart_button" );
// For AJAX add-to-cart actions
add_to_cart_button.attr( "data-quantity", jQuery( this ).val() );
// For non-AJAX add-to-cart actions
add_to_cart_button.attr( "href", "?add-to-cart=" + add_to_cart_button.attr( "data-product_id" ) + "&quantity=" + jQuery( this ).val() );
});
' );
}
add_action( 'init', 'custom_add_to_cart_quantity_handler' );
Copy code to Code Snippet

Save & activate the snippet, then test

You should be all set now! Now that you have the code function running on your website, your store page should have the quantity picker next to the add to cart buttons!

Add quantity fields to WooCommerce store page

If you have any questions and/or comments, please leave a comment below.

Leave a Reply