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

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
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x