Search code examples
phpwordpresswoocommercecart

Show only free shipping when over x amount issue in Woocommerce


I am trying to hide "flat-rate" if free shipping is available.

add_filter( 'woocommerce_package_rates', 'hide_other_shipping_when_free_is_available', 100, 2 );

function hide_other_shipping_when_free_is_available( $rates, $package ) {

    $free = array();
    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id ) {
            $free[ $rate_id ] = $rate;
            break;
        }
    }
    return ! empty( $free ) ? $free : $rates;
}

I found the following snippet and added to my functions.php, but it does not hide the flat rate shipping option.


Solution

  • This code still perfectly works for woocommerce versions 2.6+ (so also 3.2.x)

    The missing part is once you have saved your code in your function.php file, you need to refresh the shipping cached data:

    Disable, save and enable, save related shipping methods for the current shipping zone, in woocommerce shipping settings.