Enable local delivery if certain user role is logged in

99 views Asked by At

I'm currently working to create a wholesale option through woocommerce. I have the wholesale infrastructure down when you are logged in and have the user role "wholesale_customer". However I'm one step away from finishing and I can't figure it out.

I want to offer local delivery for our in town wholesale customers and that's it; not customers or guest users. Currently we have a usps plugin that offers shipping for the customers and guest purchases.

Here is currently what I have, I fee like I'm close, just missing a few things. Anyone have any suggestions?

function wholesale_local_delivery( $available_methods ) {
global $woocommerce;
if ( isset( $available_methods['local_delivery'] ) ) {
if (current_user_can('wholesale_customer')) {
unset( $available_methods['local_delivery'] );
}
}
return $available_methods;
}
add_filter( 'woocommerce_available_shipping_methods', 'wholesale_local_delivery' );

P.S. I know I could purchase the Woocommerce plugin but I don't want to do that.

1

There are 1 answers

4
mcorkum On

current_user_can doesn't check the role, it checks permissions which I don't think is what you want. Try this:

function wholesale_local_delivery($available_methods) {
    global $woocommerce;
    global $current_user;

    $user_roles = $current_user->roles;
    $user_role = array_shift($user_roles);

    if ( isset( $available_methods['local_delivery'] ) ) {
        if ($user_role == 'wholesale_customer' ) {
            unset( $available_methods['local_delivery'] );
        }
    }
    return $available_methods;
}
add_filter( 'woocommerce_package_rates', 'wholesale_local_delivery', 10, 1);