Changing product quantity on WooCommerce checkout page
תוכן עניינים
This code snippet adds important functionality to the WooCommerce checkout page. Quantity fields on the checkout page – instead of the customer seeing only the fixed quantity of each product, they can change the quantity directly on the checkout page.
This essentially allows you to skip the shopping cart page and take visitors directly to the checkout page.
Quantity fields on the checkout page
Add the code to the child theme’s functions.php file or using a snippets management plugin.
// Add Quantity Input Beside Product Name
add_filter( 'woocommerce_checkout_cart_item_quantity', 'multi_checkout_item_quantity_input', 9999, 3 );
function multi_checkout_item_quantity_input( $product_quantity, $cart_item, $cart_item_key ) {
$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 );
if ( ! $product->is_sold_individually() ) {
$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 .= '';
}
return $product_quantity;
}
// Detect Quantity Change and Recalculate Totals
add_action( 'woocommerce_checkout_update_order_review', 'multi_update_item_quantity_checkout' );
function multi_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();
}
}