Reputation: 65
I am looking for a way to disable the coupon field for wholesalers in WooCommerce on the cart and checkout pages. I am using a custom theme and have the WooCommerce Role Based Methods plug-in in conjunction with WooCommerce Wholesale Pricing. I have tried the following in functions.php
:
// hide coupon field on cart page for wholesale
function hide_coupon_field_on_cart( $enabled ) {
if( ! current_user_can( 'wholesale_customer' ) && is_cart() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_on_cart' );
// hide coupon field on checkout page for wholesale
function hide_coupon_field_on_checkout( $enabled ) {
if( ! current_user_can( 'wholesale_customer' ) && is_checkout() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_on_checkout' );
But that didn't work. What am I doing wrong?
Upvotes: 2
Views: 1743
Reputation: 5846
Try this:
function woo_get_user_role() {
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
// hide coupon field on cart page for wholesale
function hide_coupon_field_on_cart( $enabled ) {
if(woo_get_user_role() =='wholesale_customer' && is_cart() || is_checkout() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_on_cart' );
You can also merge both functions into one.
Upvotes: 3
Reputation: 16
Try this:
$current_user = wp_get_current_user();
if ( !($current_user instanceof WP_User) )
return true;
$roles = $current_user->roles;
foreach($roles as $role){
// wholesaler is your role name, not display name
if( $role == "wholesaler" ){
$isWholesalers = 1;
}
}
if(!isset($isWholesalers)){
return true;
}
if(is_cart()/*is_checkout*/ )
return false;
Upvotes: 0