2

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.

Blake
  • 561
  • 5
  • 14

1 Answers1

0

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);
mcorkum
  • 438
  • 2
  • 10
  • Still nothing. Maybe it's a priority issue? @mcorkum – Blake Jun 26 '15 at 04:35
  • Oh - I found some documentation. I think you might be using the wrong filter - Try: woocommerce_package_rates instead. Also make sure you clear WooCommerce Cache. Take a look here: https://wordpress.org/support/topic/hide-shipping-options-code-not-working-after-update-to-23 or Here: https://support.woothemes.com/hc/communities/public/questions/202313415-woocommerce-available-shipping-methods - I've updated the answer. – mcorkum Jun 26 '15 at 04:53
  • Still no luck :( I don't know what's up. – Blake Jun 26 '15 at 05:02
  • From everything I can see, it looks like this is the right way. Are you sure the user role is matching? Do a print_r($current_user) somewhere in your template and make sure the role name is matching? – mcorkum Jun 26 '15 at 13:41