Apply discount for specific user role woocommerce

Hi everybody,

I 'm trying to create a function that will give a specific discount percentage for specific user role always. I tried :

function wholesale_discount() {
  global $woocommerce;

  $woocommerce->cart->add_discount('01.1.0002.0012');
}

add_action( 'woocommerce_cart_calculate_fees','wholesale_discount' );

The first gives the discount but returns an error that the coupon has already applied while this coupon has no restrictions and works without the function.I tried with the add_fee with a negative parameter but can’t pass percentages. I was thinking playing with get_cart_subtotal() but I don’t want to reach there. I haven’t added the ifs to see what user role is logged in so don’t mind that.

Thank you in advance

1 Like

I think something like this would work:

function wholesale_discount() {
     global $woocommerce;
     $user = wp_get_current_user();

     if ( in_array( 'author', (array) $user->roles ) ) {   
          // you can specify multiple roles in they array above
          $woocommerce->cart->add_discount('01.1.0002.0012');
     }
}

add_action( 'woocommerce_cart_calculate_fees','wholesale_discount' );

I haven’t tested the function, but it should more or less be like that.

Same error as I mentioned above the coupon have been used.

Any suggestions ? Thank you

Ι found it . I used negative add fee for specific role and subtotal * 0.25. I want also the price of this to be changed .

Anyway I don’t want to display the price at cart top right so since I found the solution the topic can be closed. Thank you all.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.