← Back to All Snippets

Hide All Other Shipping Methods When Free Shipping Is Available in WooCommerce

⚡ Target: functions.php 🔒 Tested: WP 6.6+ / PHP 8.2+ 💻 Lang: PHP
PHP (functions.php)
add_filter( 'woocommerce_package_rates', 'tipwp_hide_shipping_when_free_available', 100, 2 );
function tipwp_hide_shipping_when_free_available( $rates, $package ) {
    $free_shipping = [];
    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->get_method_id() ) {
            $free_shipping[ $rate_id ] = $rate;
            break;
        }
    }
    return ! empty( $free_shipping ) ? $free_shipping : $rates;
}

Streamline checkout by hiding expensive flat-rate and local pickup options whenever the customer qualifies for Free Shipping.

← Back to All Snippets Explore Dev Tools →