Shipping Methods Reserved for Certain Roles
Hides chosen shipping methods, such as pallet delivery or trade collection, from everyone except customers with the roles you list.
Opens WPIDE on
Code
$wpide_reserved_methods = array( 'flat_rate:7' ); // Methods or rate IDs to reserve.
$wpide_reserved_roles = array( 'wholesale_customer' ); // Roles that may use them.
add_filter( 'woocommerce_package_rates', function ( $rates ) use ( $wpide_reserved_methods, $wpide_reserved_roles ) {
$allowed = is_user_logged_in() && array_intersect( $wpide_reserved_roles, (array) wp_get_current_user()->roles );
if ( $allowed ) {
return $rates;
}
foreach ( $rates as $id => $rate ) {
if ( in_array( $id, $wpide_reserved_methods, true ) || in_array( $rate->get_method_id(), $wpide_reserved_methods, true ) ) {
unset( $rates[ $id ] );
}
}
return $rates;
}, 20 );
Notes
List method IDs (local_pickup) or rate IDs (flat_rate:7); guests never see these methods.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.