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_customerorsubscriber. - 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.
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
- User Role Check: The function identifies the logged-in user’s role, such as
wholesale_customerorsubscriber. - Discount Application: The cart dynamically applies a discount to all products for users with applicable roles.
- Discount Message: Displays a notification message on the cart page, informing the user of their discount.
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.
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Stay In Touch

