How to Implement Custom Discounts Based on User Role in WooCommerce – 2026

Last updated on December 7th, 2024 at 07:39 am

Offering discounts based on user roles in WooCommerce can be a powerful strategy for rewarding loyal customers, wholesalers, or specific user groups. Whether you want to offer discounts to wholesale buyers or exclusive deals to premium members, role-based pricing can help you achieve this.

In this guide, we’ll show you how to implement custom discounts based on user roles in WooCommerce, all without using a plugin.


Step 1: Check the User Role on the Front End

The first step is to identify the current user’s role so you can apply a discount conditionally. WooCommerce integrates seamlessly with WordPress user roles, making it straightforward to fetch the user role.

Code to Check the User Role

Add the following function to your theme’s functions.php file or your child theme:

/*
 * Snippet: How to Implement Custom Discounts Based on User Role in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1753
* Tested with WooCommerce 10.7.0
* "This function checks the current user role"
*/ function wcsuccess_get_current_user_role() { if ( is_user_logged_in() ) { $user = wp_get_current_user(); $roles = (array) $user->roles; return $roles[0]; // Return the first role } return null; // Return null for non-logged-in users }

Explanation:

  • User Role Retrieval: This function returns the role of the logged-in user. For non-logged-in users, it returns null.
  • Role Hierarchy: WordPress users can have multiple roles; this function uses the first role.

Step 2: Apply Discounts Based on User Role

Once we have the user role, we can apply a discount dynamically to the cart based on the role.

Code to Apply Role-Based Discounts

Add this code to your functions.php file:

/*
 * Snippet: How to Implement Custom Discounts Based on User Role in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1753
* Tested with WooCommerce 10.7.0
* "This function applies discounts to the cart based on user role"
*/ function wcsuccess_apply_role_based_discounts( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { return; } $user_role = wcsuccess_get_current_user_role(); // Define discounts for specific roles $role_discounts = array( 'wholesale_customer' => 0.20, // 20% discount for wholesale customers 'subscriber' => 0.10, // 10% discount for subscribers ); if ( isset( $role_discounts[ $user_role ] ) ) { foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) { $product = $cart_item['data']; $original_price = $product->get_price(); $discount = $original_price * $role_discounts[ $user_role ]; $product->set_price( $original_price - $discount ); } } } add_action( 'woocommerce_before_calculate_totals', 'wcsuccess_apply_role_based_discounts', 10, 1 );

Explanation:

  • Role-Based Discounts: Defines discounts as percentages for specific roles, such as wholesale_customer or subscriber.
  • Dynamic Price Adjustment: Adjusts the product price in the cart based on the discount percentage.
  • No Impact on Regular Pricing: The original product price remains unchanged on the product page.
See also  How to Set Maximum Order Quantity Based on User Role in WooCommerce - 2026

Step 3: Display a Role-Based Discount Message

To improve user experience, display a message notifying the user about their discount.

Code to Display Discount Messages

Add this to your functions.php file:

/*
 * Snippet: How to Implement Custom Discounts Based on User Role in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1753
* Tested with WooCommerce 10.7.0
* "This function displays a notification message about the role based discount"
*/ function wcsuccess_display_role_discount_message() { $user_role = wcsuccess_get_current_user_role(); // Define messages for specific roles $role_messages = array( 'wholesale_customer' => __( 'You are receiving a 20% discount as a wholesale customer!', 'woocommerce' ), 'subscriber' => __( 'You are receiving a 10% discount as a subscriber!', 'woocommerce' ), ); if ( isset( $role_messages[ $user_role ] ) ) { wc_print_notice( $role_messages[ $user_role ], 'notice' ); } } add_action( 'woocommerce_before_cart', 'wcsuccess_display_role_discount_message' );

Explanation:

  • Custom Messages: Displays a tailored message for each user role when discounts are applied.
  • Integration: The message appears on the cart page, ensuring users are aware of their discount.

Step 4: Optional – Exclude Certain Products from Discounts

If you want to exclude certain products (e.g., sale items) from role-based discounts, you can add a condition to the discount logic.

Code to Exclude Sale Items

Update the discount logic in the wcsuccess_apply_role_based_discounts function:

if ( isset( $role_discounts[ $user_role ] ) ) {
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        $product = $cart_item['data'];

        // Skip sale items
        if ( $product->is_on_sale() ) {
            continue;
        }

        $original_price = $product->get_price();
        $discount = $original_price * $role_discounts[ $user_role ];
        $product->set_price( $original_price - $discount );
    }
}

Example Workflow

  1. User Role Check: The function identifies the logged-in user’s role, such as wholesale_customer or subscriber.
  2. Discount Application: The cart dynamically applies a discount to all products for users with applicable roles.
  3. Discount Message: Displays a notification message on the cart page, informing the user of their discount.
See also  How to Import WooCommerce Reviews Without a Plugin - 2026

Conclusion

Implementing custom discounts based on user roles in WooCommerce allows you to create tailored offers for specific user groups, increasing engagement and driving loyalty. By following this guide, you can implement role-based pricing without a plugin, maintaining control over your store’s functionality.

Test these changes on a staging environment before applying them to your live site. For more WooCommerce customisation options, explore our WooCommerce Visual Hooks Guide or use our wp-config generator for advanced configurations.

0 0 votes
Article Rating

Stay In Touch

Was this post helpful? Why not show your support and buy me a coffee?

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to Top
0
Would love your thoughts, please comment.x
()
x
×