Home / Admin / WooCommerce: Hide other shipping methods when Free Shipping is available.
Duplicate Snippet

Embed Snippet on Your Site

WooCommerce: Hide other shipping methods when Free Shipping is available.

This function checks if the Free Shipping method is available in the cart. If it is, it will hide all other shipping methods and only show the Free Shipping option.

Code Preview
php
<?php
function wpexpert28_hide_shipping_when_free_is_available( $rates ) {
    $free_shipping = array();
    
    // Loop through each shipping rate
    foreach ( $rates as $rate_id => $rate ) {
        // Check if the shipping method is Free Shipping
        if ( 'free_shipping' === $rate->get_method_id() ) {
            $free_shipping[ $rate_id ] = $rate;
        }
    }
    
    // Return only Free Shipping rates if available, otherwise return all rates
    return ! empty( $free_shipping ) ? $free_shipping : $rates;
}
// Hook into WooCommerce to filter the available shipping rates
add_filter( 'woocommerce_package_rates', 'wpexpert28_hide_shipping_when_free_is_available', 100 );

Comments

Add a Comment