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 );
2
Answers
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:
Code goes in functions.php file of the active child theme (or active theme). It should works.
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: