Add remove shipping method when free shipping class is available WooCommerce

add_filter( ‘woocommerce_package_rates’,’hide_shipping_when_free_is_available’);

function hide_shipping_when_free_is_available( $rates, $package ) {
$freeship = array();
$flatship = array();
// Only modify rates if free_shipping is present
$total = WC()->cart->cart_contents_total;
if( $total >= 150 ) {
foreach ( $rates as $rate_id => $rate ) {
if ( ‘free_shipping’ === $rate->method_id ) {
$freeship[ $rate_id ] = $rate;
break;
}
}
foreach ( $rates as $rate_id => $rate ) {

if ( ‘flat_rate’ === $rate->method_id ) {
continue;

}
$flatship[ $rate_id ] = $rate;
}
return ! empty( $freeship ) ? $flatship : $rates;
}else{
return $rates;
}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s