Update shipping on quantity change
August 7, 2025 · AI Conversations
I want to change shipping method when change quantity in checkout page
By default, WooCommerce refreshes shipping when addresses change, but not when quantity changes on checkout — you have to trigger it manually.
—
### **1️⃣ Frontend (JavaScript) – Trigger Shipping Update on Quantity Change**
WooCommerce listens for the `update_checkout` event in JavaScript to refresh shipping methods and totals.
So we just need to trigger that when a quantity changes.
jQuery(function($) {
// On checkout page only
if ($('form.checkout').length) {
$(document).on('change', '.qty', function() {
$('body').trigger('update_checkout');
});
}
});
—
### **2️⃣ PHP (Optional) – Ensure Shipping Methods Update**
If you need to manipulate available shipping methods dynamically when checkout updates, hook into:
add_action('woocommerce_checkout_update_order_review', 'custom_refresh_shipping_on_qty_change');
function custom_refresh_shipping_on_qty_change( $post_data ) {
// Parse form data from checkout
parse_str($post_data, $form_data);
// Example: force default shipping method when quantity changes
if ( isset($form_data['shipping_method']) && is_array($form_data['shipping_method']) ) {
// Here you can change it based on your own logic
WC()->session->set( 'chosen_shipping_methods', array( 'flat_rate:1' ) );
}
}
—
### **3️⃣ Using WooCommerce Update Fragments Hook**
If you also want to **update a specific part of the checkout page** along with shipping, you can use:
add_filter( 'woocommerce_update_order_review_fragments', 'custom_shipping_fragment' );
function custom_shipping_fragment( $fragments ) {
ob_start();
wc_cart_totals_shipping_html();
$fragments['.woocommerce-shipping-totals'] = ob_get_clean();
return $fragments;
}
This ensures that your **shipping totals block** gets updated during an AJAX refresh.
—
✅ **How it works in short**
– **JS** → Listens for quantity change and triggers `update_checkout`.
– **WooCommerce AJAX** → Calls backend to recalculate totals & shipping.
– **PHP hook** → Optionally sets/changes shipping method dynamically.
– **Fragments hook** → Updates shipping totals HTML without full reload.
—
If you want, I can give you **a complete ready-to-use code** that will:
– Trigger shipping update on quantity change in checkout.
– Automatically switch to your preferred shipping method based on cart quantity.
Do you want me to prepare that full code?