Hide Shipping for a User Role

The code below will remove a specified shipping option, in this case one that was generated from the Table Rate Shipping method for WooCommerce. In this scenario, the admin’s shipping options are all available to every customer, but they wish that ‘Free Shipping’ only be available to their regular customers, and not the wholesale providers.

add_filter( 'woocommerce_package_rates', 'hide_shipping_for_user_role', 10, 2 );
function hide_shipping_for_user_role( $rates, $package ) {
	// Role ID to be excluded
	$excluded_role = "wholesale_customer";

	// Shipping rate to be excluded
	$shipping_id = 'table_rate_shipping_free-shipping';

	// Get current user's role
	$user = wp_get_current_user();
	if ( empty( $user ) ) return false;

	if( in_array( $excluded_role, (array) $user->roles ) && isset( $rates[ $shipping_id ] ) )
		unset( $rates[ $shipping_id ] );

	return $rates;

Many shipping methods, including the ones that come packaged in WooCommerce, only return one shipping rate, so the shipping rate ID is often the shipping method ID. In this example, written for the Bolder Elements Table Rate method, it is the shipping method ID followed by an underscore and the value under the Identifier column for the rate that is to be removed.

  • Was this article helpful ?
  • yes   no