Whether your store has numerous user roles like customers, subscribers, or wholesalers, this snippet will help you to present shipping methods based on the user role.
add_filter( 'woocommerce_package_rates', 'ts_hide_specific_shipping_method_based_on_user_role', 100, 2 ); function ts_hide_specific_shipping_method_based_on_user_role( $rates, $package ) { // Here define the shipping rate ID to hide $targeted_rate_id = 'free_shipping:5'; // The shipping rate ID to hide $targeted_user_roles = array('customer', 'subscriber'); // The user roles to target (array) $current_user = wp_get_current_user(); $matched_roles = array_intersect($targeted_user_roles, $current_user->roles); if( ! empty($matched_roles) && isset($rates[$targeted_rate_id]) ) { unset($rates[$targeted_rate_id]); } return $rates; }
Output
If the user is logged in with the ‘customer’ or ‘subscriber’ role, they will exclusively see the ‘Free Shipping’ method.
For users with other roles, the regularly available shipping methods will be displayed.
Hide Specific Shipping Methods For Specific User Roles in WooCommerce
You might have different shipping methods in your online store and in such cases, you may have the need to hide specific shipping methods for specific user roles. This code snippet works on the scenario to hide the “Free Shipping” method for the user role “Customer” and hide the “Flat rate Shipping” method for the user role “Subscriber”. In short, it excludes one shipping method for customers and another shipping method for subscribers.
add_filter( 'woocommerce_package_rates', 'ts_hide_specific_shipping_method', 100, 2 ); function ts_hide_specific_shipping_method( $rates, $package ) { // Define the shipping rate IDs to hide $targeted_rate_ids_customer = array( 'free_shipping:5', // Free shipping method for customers ); $targeted_rate_ids_subscriber = array( 'flat_rate:4' // Flat rate shipping method for subscribers ); // Define the user roles to target $current_user = wp_get_current_user(); $user_roles = $current_user->roles; if ( in_array( 'customer', $user_roles ) ) { // If user is a customer, hide specific shipping methods for customers foreach ( $targeted_rate_ids_customer as $rate_id ) { if ( isset( $rates[ $rate_id ] ) ) { unset( $rates[ $rate_id ] ); } } } elseif ( in_array( 'subscriber', $user_roles ) ) { // If user is a subscriber, hide specific shipping methods for subscribers foreach ( $targeted_rate_ids_subscriber as $rate_id ) { if ( isset( $rates[ $rate_id ] ) ) { unset( $rates[ $rate_id ] ); } } } return $rates; }
When a customer logs in as a customer role, the code tends to hide the “Free shipping” shipping method. If you have specified the specific shipping rate id of a particular zone such as “free shipping:5”, the code hides the shipping method for that specific zone.
So when the subscriber user has logged in to our site, as specified in the code, it will hide the flat rate shipping method.
Hide Certain Shipping Methods For Guest Users in WooCommerce
This code snippet helps you to customize the visibility options of the available shipping methods based on the user roles. For example, the code tends to hide both the “Flat rate” and “Free shipping” of a particular shipping zone when users are not logged in and visit the site as guest users.
add_filter( 'woocommerce_package_rates', 'ts_hide_specific_shipping_method', 10, 2 ); function ts_hide_specific_shipping_method( $rates, $package ) { // Define the shipping rate IDs to hide for guests $targeted_rate_ids_guest = array( 'flat_rate:4', // Flat rate shipping method for guests 'free_shipping:5' // Free shipping method for guests ); // Check if user is not logged in (guest user) if ( ! is_user_logged_in() ) { // If user is not logged in, hide specific shipping methods for guests foreach ( $targeted_rate_ids_guest as $rate_id ) { if ( isset( $rates[ $rate_id ] ) ) { unset( $rates[ $rate_id ] ); } } } return $rates; }
When the customer visits the site as guest user, the code will hide certain shipping methods for guest users (not logged in) and show only specific shipping methods.
Shipping method to hide as specified in the code are:
Shipping method | Cost |
flat_rate: 4 | $10 |
free_shipping: 5 | Free |
So, the user who is not logged in will only be able to see the “local pickup” option in the cart totals section of the cart page.
Similarly, you can also disable shipping methods if user is logged out in WooCommerce which will encourage users to create an account in your store.
Hello there,
I want to exclude 1 shipping method for customers and 1 shipping method for subscribers, but i cant execute that because it says: Cannot redeclare function ts_hide_specific_shipping_method_based_on_user_role.
How can i fix this?
Hi Dennis,

The post has been updated for your specific requirement. Please check the code under the heading “Hide Specific Shipping Methods For Specific User Roles in WooCommerce”.
Hi Saranya,
Thank you for the quick response. It is working now for the subscribers and for the customers, but the main portion of my buyers are people with arent logged in, so they have no account. How can i target them?
Hi Dennis,
The post has been updated for your specific requirement. Please check the code under the heading “Hide Certain Shipping Methods For Guest Users in WooCommerce”.
Thank you so much so far, but how can i combine these 2 rules? So show and hide a specific shipping method for subscribers and guest?
Hi Dennis,
Please refer to the code from this gist that will hide ‘Flat Rate’ shipping method for guest users and ‘Free Shipping’ shipping method for subscriber user roles.
Wow thanks it works now for guest and subscribers. Can i copy this part to also hide a certain part for clients?
// Define the shipping rate IDs to hide for client$targeted_rate_ids_client = array( ‘free_shipping:5’ // Add the shipping rate IDs to hide for subscribers here );
Hi Dennis, If you have created a custom user role named “Client” either through custom coding or by using a plugin, then in addition to the part that you mentioned in the comment, you should also use the appropriate slug ‘client’ to check for the custom role. The below code will serve as a reference code to target users with the client role and hides the Free Shipping method accordingly. add_filter( 'woocommerce_package_rates', 'ts_hide_specific_shipping_method', 10, 2 ); function ts_hide_specific_shipping_method( $rates, $package ) { // Define the shipping rate ID to hide for clients $targeted_rate_id_client = 'free_shipping:5'; // Replace '5' with the… Read more »