Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Hide Shipping Methods Based on User Roles in WooCommerce?

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.

How to Hide Shipping Methods Based on User Roles in WooCommerce

For users with other roles, the regularly available shipping methods will be displayed.

How to Hide Shipping Methods Based on User Roles in WooCommerce? - Tyche Softwares

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.

How to Hide Shipping Methods Based on User Roles in WooCommerce? - Tyche Softwares

So when the subscriber user has logged in to our site, as specified in the code, it will hide the flat rate shipping method.

How to Hide Shipping Methods Based on User Roles in WooCommerce? - Tyche Softwares

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: 5Free

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.

How to Hide Shipping Methods Based on User Roles in WooCommerce? - Tyche Softwares

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.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
8 Comments
Newest
Oldest
Inline Feedbacks
View all comments
Dennis
8 months ago

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?

Dennis
8 months ago
Reply to  Saranya

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?

Dennis
8 months ago
Reply to  Saranya

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?

Dennis
8 months ago
Reply to  Saranya

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 );

8
0
Would love your thoughts, please comment.x
()
x