Give a discount on the total price based on User Roles!

In this article we will find out how to give a discount on total price based on user roles.

Sometimes you may want to give discounts to your customer based on your user roles on your Woocommerce store, Like a wholesaler, Retailer, Vendors on cart total. Few lines of code will make a trick, it will full fill your requirement.

Copy this below code and paste in on your theme function.php file.

add_action( 'woocommerce_cart_calculate_fees', 'tw_discount_price' );
    function tw_discount_price() {
        global $woocommerce;

       //Set the price for user role.
       $discount_price = 5;

      //Get the login user role
      $user = wp_get_current_user();
      $user_role = $user->roles;  
  
     //Check the specifc user role for discount. 
     if( $user_role[0] == 'administrator' || $user_role[0] == 'vendor' ){
        $woocommerce->cart->add_fee( 'Discounted Price', -$discount_price, true, 'standard' );
    }

}

Now before adding the code snippet the cart shows without discount.

After adding the code snippet: 

Hope this article fulfilled your business requirement. If you need help setting up the plugin and the code snippet you can always contact our team.

Leave a Reply