禁用除“wholesale_customer”以外的所有人的本地发送

Alright, I've been pulling my hair out the last couple of days trying to figure this out.

I have a wholesale plugin in wordpress install with woocommerce. It gives the user "wholesale_customer" special rates over everyone else. I want to be able to offer local delivery to only the "wholesale_customer" user role but can't seem to figure out how to do it.

I've gotten this code from @mcorkum but it's still not working.

/**
 * Add local delivery for wholesale customers
 */
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);

I know this achievable with a plugin, but I'd rather not use plugins or pay for it for that matter.

Does anyone see anything that I'm not seeing?

/**
 * Add local delivery for wholesale customers
 */
function wholesale_local_delivery($available_methods) {
    global $woocommerce;
    global $current_user;
    if ( isset( $available_methods['local_delivery'] ) ) {
        if ( !current_user_can( 'wholesale_customer' ) ) {
            unset( $available_methods['local_delivery'] );
        }
    }
    return $available_methods;
}
add_filter( 'woocommerce_package_rates', 'wholesale_local_delivery', 10, 1);

Try the above code by pasting it in your theme's functions.php file. And let me know if this worked for you.

I'm not a wordpress dev, but that code doesn't look like it is providing a user with role "wholesale_customer" the "local_delivery" option. On the contrary in fact, it looks to be removing the local delivery option if the user role IS "wholesale_customer":

if ( isset( $available_methods['local_delivery'] ) ) {
    if ($user_role == 'wholesale_customer' ) {
        unset( $available_methods['local_delivery'] );
    }
}

If I was to take this code simply at face value (As I am not a wordpress dev) I would re-write this function to be easier to understand and read:

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

    // Return early if no local delivery option is available
    if (!isset($available_methods['local_delivery'])) {
        return $available_methods;
    }

    // Determine if the user has a user role of wholesale customer
    $hasRoleWholeSaleCustomer = false;
    foreach ($current_user->roles as $role) {
        if ($role === 'wholesale_customer') {
            $hasRoleWholeSaleCustomer = true;
            break;
        }
    }

    // If the user does not have the role wholesale customer
    // And for the code here to be being processed the local delivery
    // option must be available
    if (!$hasRoleWholeSaleCustomer) {
        unset($available_methods['local_delivery']);
    }

    // Return the available methods applicable to the users roles
    return $available_methods;
}

Hope someone else with experience in woocomerce, can give a better answer. But in the meantime, you can try this re-write and see if it works for you.

Goodluck.