By implementing this code, the store owner can decide when to offer free shipping based on certain rules. Free shipping is the only rate displayed if the cart subtotal meets or exceeds this minimum amount. If the threshold is not met, it hides only free shipping from the available shipping methods.
add_filter('woocommerce_package_rates', 'ts_filter_package_rates', 10, 2 ); function ts_filter_package_rates( $rates, $package ) { $currency = get_woocommerce_currency(); $free = array(); foreach ( $rates as $rate_key => $rate ) { // For "free_shipping" if ( 'free_shipping' === $rate->method_id ) { $cart_total = WC()->cart->subtotal; // (or WC()->cart->get_subtotal() without taxes) $min_amount = 100; // Free shipping min amount $free_shipping = new \WC_Shipping_Free_Shipping( $rate->instance_id ); $applied_coupons = WC()->cart->get_applied_coupons(); if ( 'either' === $free_shipping->requires && ! empty($applied_coupons) && $cart_total < $min_amount ) { foreach ( $applied_coupons as $coupon_code ) { $coupon = new WC_Coupon( $coupon_code ); if( $coupon->get_free_shipping() ) { $coupon_free_ship = true; break; } } } // Enable free shipping for a minimal cart amount or a coupon with free shipping option if( $cart_total < $min_amount && ! isset($coupon_free_ship) ) { unset($rates[$rate_key]); } else { $free[$rate_key] = $rate; break; } } } return ! empty( $free ) ? $free : $rates; }
Output
When the cart subtotal reaches the minimum amount or greater than it, then the free shipping option is the only option displayed to customers.
As the cart subtotal is below the threshold amount specified in the code, the “free_shipping” method is unset (removed) from the list of available shipping rates.
Additionally, you can also set a minimum order amount based on shipping zone and disabling checkout page if not met helping you in restricting order placement.