How can I apply a Woocommerce Coupon to a specific user role without a plugin?

I have implemented a wholesale user role to my Wordpress client site. The ultimate goal is for the wholesale user to have 40% off all products, but if they spend $ 500 or more, they get an extra 7% off their total cart purchase. I set up an initial 40% discount with dynamic pricing and for an additional 7% I created a coupon to automatically apply to the cart without having to enter a coupon code.

The only problem is that the coupon works for all users (customers, admins and dealers) and is independent of the specific role. Can anyone tell me how I can change my coupon code to only apply to the user role for "dealer"? If you need to see a live site, you can see what is here ! Thank!

    add_action( 'woocommerce_before_cart', 'apply_matched_coupons' );

function apply_matched_coupons() {
    global $woocommerce;

    $coupon_code = 'additionaldiscount'; // coupon code

    if ( $woocommerce->cart->has_discount( $coupon_code ) ) return;

    if ( $woocommerce->cart->cart_contents_total >= 500 ) {
        $woocommerce->cart->add_discount( $coupon_code );
        $woocommerce->show_messages();
    }

}

      

+3


source to share


1 answer


You can use current_user_can()

to check a role or capabilities:



if ( current_user_can('dealer') && $woocommerce->cart->cart_contents_total >= 500 ) {
    $woocommerce->cart->add_discount( $coupon_code );
    $woocommerce->show_messages();
}

      

+2


source







All Articles