# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, shipping, roles, b2b
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-shipping-method-for-roles/

## Code

```php
$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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Shipping Methods Reserved for Certain Roles" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
