3

I have a code to hide shipping for non-wholesale customers, please help me redo it, I need to hide the shipping option for wholesale customers.

/**
 * Removes shipping methods for non-wholesale customers.
 * Please be sure to clear your WooCommerce store's cache.
 * Adjust 'flat_rate:2' to match that of your wholesale shipping method.
 */
 
function my_wcs_remove_shipping_non_wholesale( $rates, $package ){
    global $current_user;

    $is_wholesale = get_user_meta( $current_user->ID, 'wcs_wholesale_customer', true );

    if ( ! $is_wholesale ) {
        foreach( $rates as $method ) {
            if ( $method->id == 'flat_rate:2' ) {
                unset( $rates[$method->id] );           
            }
        }
    }
    
    return $rates;
}
add_filter( 'woocommerce_package_rates', 'my_wcs_remove_shipping_non_wholesale', 10, 2 );
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Spirit
  • 43
  • 3
  • Simply replace `if ( ! $is_wholesale ) {` by `if ( $is_wholesale ) {` to target wholesale customers – LoicTheAztec Nov 01 '20 at 16:11
  • How easy it is! Yes it worked! Thank you, you are the best! – Spirit Nov 01 '20 at 16:25
  • @LoicTheAztec Sorry for the stupid question, I am trying to run 2 scripts at the same time but I get the error The snippet has been deactivated due to an error on line 8: Cannot redeclare function my_wcs_remove_shipping_non_wholesale. , How do I rename the function correctly so that everything works at the same time?thank you :) – Spirit Nov 01 '20 at 16:44
  • unfortunately I'm not a programmer and I can't run 2 identical scripts, please help – Spirit Nov 01 '20 at 17:46
  • @LoicTheAztec It's just incredible. Programming is power! If I get rich I will not forget about you! Thank you very much! You saved me :-) – Spirit Nov 01 '20 at 18:11
  • @LoicTheAztec yes it works :) i accept the answer, Loic the best! – Spirit Nov 01 '20 at 18:30

2 Answers2

1

You don't need to have 2 functions, one for Wholesale customers and an other for non Wholesale customers… you can merge both in the same function as follows:

add_filter( 'woocommerce_package_rates', 'shipping_methods_based_on_wholesale_customer', 10, 2 );
function shipping_methods_based_on_wholesale_customer( $rates, $package ){
    $is_wholesale = get_user_meta( get_current_user_id(), 'wcs_wholesale_customer', true );
    
    // Set the shipping methods rate ids in the arrays:
    if( $is_wholesale ) {
        $shipping_rates_ids = array('flat_rate:1', 'flat_rate:4'); // To be removed for NON Wholesale users
    } else {
        $shipping_rates_ids = array('flat_rate:2'); // To be removed for Wholesale users
    }

    // Loop through shipping rates fro the current shipping package
    foreach( $rates as $rate_key => $rate ) {
        if ( in_array( $rate_key, $shipping_rates_ids) ) {
            unset( $rates[$rate_key] ); 
        }
    }
    
    return $rates;
}

Code goes in functions.php file of the active child theme (or active theme). It should works.

Don't forget to empty the cart after saving the code, to refresh cached shipping data

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hello Dear Loic , thank you very much for your help! Sorry to disturb you again, this code is working but when I create a new role and substitute a new value for "wholeseller" $is_wholesale = get_user_meta( get_current_user_id(), 'wholeseller, true ); it only works for non-wholesalers, how can I make it work fine the same for newly created roles? The plugin started to conflict with other plugins so I need to apply this code for a new role – Spirit Nov 28 '20 at 10:50
  • 1
    @Spirit For user roles you can try to use instead `$is_wholesale = current_user_can( 'wholeseller' );` – LoicTheAztec Nov 28 '20 at 11:53
0

So for everybody, like me, where the code didn't work. With the help of @Howard E here is the adjusted code 2022 which now works:

add_filter( 'woocommerce_package_rates', 'shipping_methods_based_on_wholesale_customer', 10, 2 );
function shipping_methods_based_on_wholesale_customer( $rates, $package ) {
   $user  = wp_get_current_user();
   $roles = (array) $user->roles;
   // Set the shipping methods rate ids in the arrays.
   if ( ! in_array( 'wholesale_customer', $roles, true ) ) {
      $shipping_rates_ids = array( 'flat_rate:10', 'flat_rate:7' ); // To be removed for NON Wholesale users.
   } else {
      $shipping_rates_ids = array( 'flat_rate:13', 'flat_rate:15' ); // To be removed for Wholesale users.
   }
   // Loop through shipping rates from the current shipping package.
   foreach ( $rates as $rate_key => $rate ) {
      if ( in_array( $rate_key, $shipping_rates_ids, true ) ) {
         unset( $rates[ $rate_key ] );
      }
   }
   return $rates;
}
Martijn
  • 1
  • 6