WooCommerce: Change Product Quantity @ Checkout Page

We covered a lot of WooCommerce Checkout customization in the past – it’s evident that the Checkout is the most important page of any WooCommerce website!

Today we’ll code a nice UX add-on: how do we show product quantity inputs beside each product in the Checkout order table? This is great if people need to adjust their quantities on the checkout before completing their order; also, it’s helpful when you have no Cart page and want to send people straight to Checkout and skip yet another click.

In this post, we’ll see how to add a quantity input beside each product on the Checkout page, and then we’ll code a “listener” to make sure we actually refresh the Checkout and update totals after a quantity change. Enjoy!

You can clearly see that there is a new quantity input inside the “Order Review” section of the WooCommerce Checkout page. On change, the Checkout will refresh and update totals.

PHP Snippet: Display Product Quantity Selectors @ WooCommerce Checkout

Note – you may also need to hide the “x2” string that WooCommerce displays beside the product name in case there are 2 items in the Cart (“Product Name x1”, “Product Name x2”, “Product Name x3”, etc. depending on quantity). You can do so with a neat 1-liner you find at the beginning of the snippet below.

/**
 * @snippet       Item Quantity Inputs @ WooCommerce Checkout
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 6
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

// ----------------------------
// Hide Quantity String Beside Product Name

add_filter( 'woocommerce_checkout_cart_item_quantity', '__return_empty_string' );

// ----------------------------
// Add Quantity Inputs
  
add_filter( 'woocommerce_cart_item_subtotal', 'bbloomer_checkout_item_quantity_input', 9999, 3 );

function bbloomer_checkout_item_quantity_input( $product_quantity, $cart_item, $cart_item_key ) {
  	if ( is_checkout() ) {
		$product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
		$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );	
		$product_quantity = woocommerce_quantity_input( array(
			'input_name'  => 'shipping_method_qty_' . $product_id,
			'input_value' => $cart_item['quantity'],
			'max_value'   => $product->get_max_purchase_quantity(),
			'min_value'   => '0',
		), $product, false );
		$product_quantity .= '<input type="hidden" name="product_key_' . $product_id . '" value="' . $cart_item_key . '">';
	}
	return $product_quantity;
}

// ----------------------------
// Detect Quantity Change and Recalculate Totals

add_action( 'woocommerce_checkout_update_order_review', 'bbloomer_update_item_quantity_checkout' );

function bbloomer_update_item_quantity_checkout( $post_data ) {
	parse_str( $post_data, $post_data_array );
	$updated_qty = false;
	foreach ( $post_data_array as $key => $value ) {	
		if ( substr( $key, 0, 20 ) === 'shipping_method_qty_' ) {			
			$id = substr( $key, 20 );   
			WC()->cart->set_quantity( $post_data_array['product_key_' . $id], $post_data_array[$key], false );
			$updated_qty = true;
		}		
	}	
	if ( $updated_qty ) WC()->cart->calculate_totals();
}

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

6 thoughts on “WooCommerce: Change Product Quantity @ Checkout Page

  1. Nice snippet, but i have some problem, food ordering site, when customer order same product but different addons, only last frome same products on cart work + – , when all products different , works on all. Some idea for fix ? Thanks

    https://prnt.sc/QQHyjlQW3qRU

    1. Thanks Bosko. In such case I believe a custom integration needs to be developed in order to make it work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

      1. hello! great snippet, it works for me, but the display of the price in the product line is not enough, as it is displayed without the snippet. It is especially convenient for the client to see the price too when there are several items in the checkout.

        Can you tell me what code to add so that the product amount is also displayed?

        1. Hi Volodymyr, price is meant to update when you update the quantity (checkout does refresh). Did you change anything in my snippet?

          1. It works perfectly but it doesn’t show price in line. Only subtotal. Can you edit snippet please?

            1. Not sure I follow. Can you send me a screenshot of the checkout page without and with the snippet active, and show me the inline price?

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

Your email address will not be published.