Hide all Shipping Methods but Free Shipping

Normally, all shipping methods are displayed in the cart, even if free shipping is available. Then the customer has to choose which shipping method they want. In this tutorial we will show you how to hide the other shipping methods when free shipping is available and two other combinations. To do this, you need to add the relevant code snippet to your functions.php file.

 

Only Show Free Shipping

If you want to hide all shipping methods except free shipping in cart, use this code:

function arrow_design_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;
}
add_filter( 'woocommerce_package_rates', 'arrow_design_hide_shipping_when_free_is_available', 100 );

Only Show Local Pickup and Free Shipping

This code hides all shipping methods except free shipping and local pickup.

function arrow_design_hide_shipping_when_free_is_available( $rates, $package ) {
$new_rates = array();
foreach ( $rates as $rate_id => $rate ) {
// Only modify rates if free_shipping is present.
if ( 'free_shipping' === $rate->method_id ) {
$new_rates[ $rate_id ] = $rate;
break;
}
}

if ( ! empty( $new_rates ) ) {
//Save local pickup if it's present.
foreach ( $rates as $rate_id => $rate ) {
if ('local_pickup' === $rate->method_id ) {
$new_rates[ $rate_id ] = $rate;
break;
}
}
return $new_rates;
}

return $rates;
}

add_filter( 'woocommerce_package_rates', 'arrow_design_hide_shipping_when_free_is_available', 10, 2 );

Show only Free Shipping in all States except…

This code allows you to display free shipping in all but selected states (in the list stored in the variable $excluded_states).

add_filter( 'woocommerce_package_rates', 'arrow_design_hide_all_shipping_when_free_is_available' , 10, 2 );

function arrow_design_hide_all_shipping_when_free_is_available( $rates, $package ) {

$excluded_states = array( 'AK','HI','GU','PR' );
if( isset( $rates['free_shipping'] ) AND !in_array( WC()->customer->shipping_state, $excluded_states ) ) :
// Get Free Shipping array into a new array
$freeshipping = array();
$freeshipping = $rates['free_shipping'];

// Empty the $available_methods array
unset( $rates );

// Add Free Shipping back into $avaialble_methods
$rates = array();
$rates[] = $freeshipping;

endif;

if( isset( $rates['free_shipping'] ) AND in_array( WC()->customer->shipping_state, $excluded_states ) ) {

// remove free shipping option
unset( $rates['free_shipping'] );

}

return $rates;
}

Hide Shipping Methods with Plugin

You can also use the plugin “WC Hide Shipping Methods” to display either free shipping only or free shipping and local pickup.

You can download the plugin here: WC Hide Shipping Methods

Conclusion

We hope this Hide all Shipping Methods but Free Shipping was helpful and you are now able to use this also on your own website! You can find more articles here:

 

Read another Woocommerce post : ‘Limit Woocommerce Order Note Length

 

Arrow Design, based in Dublin, Ireland, provides quality website design services in Dublin and beyond at affordable prices. If you would like help with implementing the above code, or any wordpress website development project, contact us. We love website design and it shows! We provide custom wordpress plugin development, website design training and lots more.

We do it all, so you don’t have to!

Related Posts

Woocommerce Account Page Hook Guide

In this article you will find a visual hook guide for the Woocommerce Account Pages, like the Login/Register page, the Downloads page or the Orders page.

Woocommerce PHP – Product Categories by Product ID

Woocommerce Single Product Page Hook Guide

In this article you will find a visual hook guide for the Woocommerce Single Product Page. This should help you to quickly and easily find the hook positions on the page.

Woocommerce PHP – Product Categories by Product ID

Woocommerce PHP – Product Categories by Product ID

In this tutorial you will learn how to check if a product is assigned to a tag, a category or a custom taxonomy. You can check if a product is is on the shop page, in the cart, in an order & more.

…We do more, so you can do less 🙂

 

Leave a message and we will be straight back to you!

Pin It on Pinterest

Share This