WooCommerce is such a fantastic plugin for running an online store, and it gives us a great system to manage shipping options. However, we had a client who didn't want a specific shipping option from USPS to show up, so we needed to refine the customer's checkout experience. Hiding some shipping methods has its perks, like streamlining the checkout process or encouraging customers to choose certain shipping options. We can easily do this by using the shipping method's ID.

Why Would I Hide a Shipping Method in The First Place?

Enhanced Checkout Experience: Not every shipping option always needs to be shown. Keep it simple. Sometimes, it makes sense to hide specific shipping options to declutter the checkout process. An easy process that doesn't’ require the customer to sort through many options helps to ensure that your customer completes the checkout. 

Promoting Your Preferred Shipping Method: Sometimes, it’s simply that your profit margins are higher with certain shipping options, and you want to highlight those. 

Location Restrictions: Location matters. Imagine how frustrating it would be living in Alaska and seeing a same-day delivery option when, in fact, shipping is coming from New England and will take weeks. In this scenario, you definitely need to hide same-day delivery. 

Finding the Shipping Method IDs

Before we can start hiding shipping methods, we need to find their ID codes. Here are the steps to follow to find those: 

  1. Go to Settings > Shipping
  2. Click on the “Shipping Zones” tab
  3. Select the zone(s) where you want to hide certain shipping methods
  4. Locate the specific shipping methods you want to hide
  5. Locate the “Method ID” for each of your selected shipping methods. The ID is usually displayed next to the method name or in the details section.

Remove Shipping Options By ID Code

You’ve got a couple of options for doing this in code. Here are a couple of code snippets that show you how to hide shipping methods by ID. 

Use the woocommerce_package_rates Filter: this method allows you to filter out shipping methods based on specific conditions. 

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

function custom_hide_shipping_methods( $rates, $package ) {
  $hidden_methods = array( 'flat_rate:1', 'local_pickup:2' ); // Replace with your specific shipping method IDs

  foreach ( $rates as $rate_id => $rate ) {
    if ( in_array( $rate->id, $hidden_methods ) ) {
      unset( $rates[$rate_id] );
    }
  }

  return $rates;
}

Line-by-Line Breakdown

  • Adds a custom function custom_hide_shipping_methods to the woocommerce_package_rates filter.
  • The function custom_hide_shipping_methods takes in two arguments: 
    • $rates: an array which contains all available shipping methods
    • $package: details about the shopping cart
  • $hidden_methods is an array that contains the ID codes of the shipping methods you want to hide 
  • The function then loops through each of those shipping methods
  • If the current rate’s ID matches any of the IDs we want to hide, that shipping method is “unset” from the $rates array. This hides the shipping rate from the checkout process

Use Conditional Statements: this method hides shipping methods if certain conditions are met.

add_action( 'woocommerce_cart_totals_before_shipping', 'conditional_hide_shipping_methods' );

function conditional_hide_shipping_methods() {
  if ( WC()->cart->get_subtotal() < 50 ) {
    remove_action( 'woocommerce_shipping_method_free_shipping', 'woocommerce_free_shipping_is_available', 10 );
  }
}

Line-by-Line Breakdown

  • The action hook woocommerce_cart_totals_before_shipping fires before shipping costs are calculated
  • The hide_shipping_methods checks to see is a specific condition is met by using a simple if statement
  • WC()->cart->get_subtotal()) gets the cart’s subtotal, and we check to see if that is less than 50. 
  • If that condition is met, we remove the shipping method for orders less than 50 using the remove_action function

More Advanced Conditional Hiding

The last example used a basic conditional statement to check for a certain price. However, you can make the condition statement for more elaborate scenarios such as:

  • Customer Location: If you’re using a geolocation plugin, you can easily hide certain shipping methods based on a customer’s location. 
  • Product: Depending on what product the customer has in their cart, you can hide shipping methods based on things like dimensions and weight. 

Hiding shipping methods by ID is pretty straightforward. Just make sure you leave some options for customers as every customer has a unique set of needs and wants. By using the above techniques you will be able to give your customers a more personalized and streamlined checkout process. And hopefully, this all leads to more money in your pocket.