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?
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 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:
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.