WooCommerce Remove other methods if free shipping is available

add_filter( 'woocommerce_package_rates', 'wpe_hide_shipping_when_free_is_available', 100 );

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