How to Hide Woocommerce Shipping Methods (Conditionally) – A Complete Guide

In this full complete guide, I will show you how to hide Woocommerce shipping methods conditionally without any extra plugin. Before you jump in a quick comment though: all the snippets shown below will go to either to your child theme’s functions.php file or, better yet, use Code Snippets plugin for it.

WpCodeBox is my favorite code snippets manager for WordPress. This is a premium plugin and if you’re interested, then grab WPCodeBox with a nice 20% discount here (SAVE 20% Coupon WPSH20).

Video: How to Conditionally Hide Woocommerce Shipping Methods

If you’re not good at adding code snippets on your site, then take a look at this video.

How to disable Woocommerce shipping methods for a specific shipping class?

Now, there are two things you would need to do beforehand:

  • Add a shipping class. Go to Woocommerce >> Settings >> Shipping >> Shipping classes and add one. For example: “Courier only”
  • Write down the shipping class slug because you will need it later. Where to get the slug? See the screenshot below.
  • Add shipping class to the needed product. Open your product and see under Shipping tab.
How to Hide Woocommerce shipping methods for specific shipping classes?

Next, go to Snippets >> Add new and paste this snippet here below inside your code box. Pay attention though that you need to change the shipping class slug on line 4 accordingly.

So, if in this example I have a “Courier only” shipping class with a slug “courier-only”, then replace the slug in the code accordingly with the correct one.

One more thing, on line 12 there is a shipping method that you are hiding (Flat rate). Change the shipping method name and ID as needed. How to find your shipping ID number? Well, take a look at the video above (see the 3min 44sec mark).

/* Hide WooCommerce shipping method for a specific shipping class */
function hide_flat_rate_shipping( $rates, $package ) {
    // Shipping class IDs that need the method removed
    $shipping_classes = array('pakk1'); // Here goes your shipping class slug
    $if_exists = false;
 
    foreach( $package['contents'] as $key => $values ) {
        if( in_array( $values['data']->get_shipping_class(), $shipping_classes ) )
            $if_exists = true;
    }
 
    if( $if_exists ) {
        unset( $rates['montonio_venipak_courier:4'] ); // Here goes your shipping method that needs to be hidden. Don’t forget to add the correct one here
    }
 
    return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_flat_rate_shipping', 10, 2 );

If you would like to know how to hide Woocommerce multiple shipping methods using shipping classes, then see my previous post here.

How to Hide Woocommerce Checkout Fields When Local Pickup is Selected?

One of the most annoying things with the Woocommerce shiping system is that if Local Pickup shipping method is chosen then you still need to fill in all the address and postcode fields. But what if you could automatically hide those fields if Local pickup method is chosen?

<span role="button" tabindex="0" data-code="/* This piece of code will hide fields for the chosen method.
.hide_pickup {
display: none !important;
}
*/

// Hide Local Pickup shipping method
add_filter( 'woocommerce_checkout_fields', 'hide_local_pickup_method' );
function hide_local_pickup_method( $fields_pickup ) {
// change below for the method
$shipping_method_pickup ='local_pickup:2';
// change below for the list of fields. Add (or delete) the field name you want (or don’t want) to use
$hide_fields_pickup = array( 'billing_company', 'billing_country', 'billing_postcode', 'billing_address_1', 'billing_address_2' , 'billing_city', 'billing_state');

$chosen_methods_pickup = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping_pickup = $chosen_methods_pickup[0];

foreach($hide_fields_pickup as $field_pickup ) {
if ($chosen_shipping_pickup == $shipping_method_pickup) {
$fields_pickup['billing'][$field_pickup]['required'] = false;
$fields_pickup['billing'][$field_pickup]['class'][] = 'hide_pickup';
}
$fields_pickup['billing'][$field_pickup]['class'][] = 'billing-dynamic_pickup';
}
return $fields_pickup;
}
// Local Pickup – hide fields
add_action( 'wp_head', 'local_pickup_fields', 999 );
function local_pickup_fields() {
if (is_checkout()) :
?>

  • How to Create Custom Product Badges for Woocommerce?
  • How to filter WooCommerce admin products by on sale?
  • How to rename, remove, reorder and add Woocommerce My Account tabs?
  • How to Customize Woocommerce Admin Dashboard? 16 hacks
  • How to Customize Woocommerce Checkout page? 29 useful hacks

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top