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.
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.