How to add quantity selector at checkout in WooCommerce

woo expert

The product quantity selector is usually only available on the product page and in the cart, but what if you also want to offer the customer to change the quantity of units on the checkout page?

There are multiple reasons why you would want to add this functionality to your online store, such as if you have created a redirect so that customers do not go through the cart and go directly to the checkout, or simply because you want to offer full flexibility to add or remove quantities of products at any time, you will know.

The thing is that it is very easy, you just have to add some code to your customizations plugin or to the functions.php file of the active theme, like this:

/* Quantity selector at checkout on Woo */
// We hide the quantity string next to the product name.
add_filter( 'woocommerce_checkout_cart_item_quantity', '__return_empty_string' );
// We add the quantity selector
add_filter( 'woocommerce_cart_item_subtotal', 'wphelp_selector_quantity_checkout', 9999, 3 );
function wphelp_selector_quantity_checkout( $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;
}
// We detect the change of quantity to recalculate the totals.
add_action( 'woocommerce_checkout_update_order_review', 'wphelp_recalculate_total_selector_quantity_checkout' );
function wphelp_recalculate_total_selector_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();
}Code language: PHP (php)

Save the changes and you’ll move on from this…

default checkout

To this other…

quantity checkout

How useful was this post?

Click on a smiley to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Skip to content