WooCommerce: Set role‑based pricing (simple example)
WooCommerce: Set Role-Based Pricing (Simple Example)
If you want to offer different product prices to customers based on their user roles in WooCommerce, this guide will help you implement role-based pricing quickly and effectively. By default, WooCommerce does not support pricing variations by user role, but with a small code snippet, you can customize prices for roles like wholesale customers, subscribers, or any custom role.
Quick Fix: How to Set Role-Based Pricing in WooCommerce
- Identify the user roles you want to target.
- Add a code snippet to your theme’s
functions.php
or a custom plugin. - Customize the price adjustments in the snippet according to your roles.
- Test by logging in as users with different roles to verify prices change accordingly.
Why This Happens
WooCommerce uses a single price field per product by default, which applies universally to all customers. It does not differentiate pricing based on user roles or customer groups out of the box. To offer role-based pricing, you need to programmatically adjust the displayed price depending on the logged-in user’s role. This is commonly needed for wholesale stores, membership discounts, or tiered pricing strategies.
Requirements
- WooCommerce installed and activated on your WordPress site.
- Basic knowledge of editing theme files or creating a simple plugin.
- Access to your site’s file system or WordPress admin editor.
- Understanding of user roles in WordPress/WooCommerce.
Step-by-Step: Implement Role-Based Pricing in WooCommerce
- Backup your site: Always back up your site before editing code.
- Access your theme’s
functions.php
file: Use FTP, cPanel File Manager, or WordPress Appearance > Theme Editor. - Copy and paste the following code snippet at the end of
functions.php
:
/**
* Adjust WooCommerce product price based on user role.
*/
add_filter( 'woocommerce_product_get_price', 'custom_role_based_price', 10, 2 );
add_filter( 'woocommerce_product_get_regular_price', 'custom_role_based_price', 10, 2 );
add_filter( 'woocommerce_product_get_sale_price', 'custom_role_based_price', 10, 2 );
function custom_role_based_price( $price, $product ) {
if ( is_admin() ) {
return $price; // Do not change price in admin area
}
if ( ! is_user_logged_in() ) {
return $price; // Only modify price for logged-in users
}
$user = wp_get_current_user();
$roles = (array) $user-roles;
// Example role-based pricing adjustments
if ( in_array( 'wholesale_customer', $roles ) ) {
// 20% discount for wholesale customers
$price = $price * 0.8;
} elseif ( in_array( 'subscriber', $roles ) ) {
// 10% discount for subscribers
$price = $price * 0.9;
}
return $price;
}
- Save the file.
- Test your changes: Log in as a user with the
wholesale_customer
role and check product prices. Then test with asubscriber
and a non-logged-in user to confirm pricing differences.
Common Pitfalls
- Editing the wrong file: Always use a child theme or a custom plugin to avoid losing changes during theme updates.
- Role names mismatch: Ensure the role slugs you use in the code exactly match the roles assigned to users.
- Price caching: Some caching plugins or server-side caches may serve cached prices. Clear caches after applying changes.
- Admin area price changes: The snippet avoids changing prices in the admin to prevent confusion during product editing.
- Not handling variable products: This example works for simple products. Variable products require additional handling.
Works On
Environment | Compatibility |
---|---|
Web Servers | Apache, Nginx, LiteSpeed |
Hosting Panels | cPanel, Plesk, DirectAdmin |
WordPress Versions | 5.0 and above |
WooCommerce Versions | 3.0 and above |
FAQ
- Q: Can I add more roles or different discounts?
- A: Yes. Simply add more
elseif
blocks inside the function checking for other roles and set your desired price adjustments. - Q: How do I create a custom user role like
wholesale_customer
? - A: Use a role management plugin like “User Role Editor” or add code to register a new role with
add_role()
in WordPress. - Q: Does this work with variable products?
- A: This snippet targets simple products. Variable products require hooking into variation prices separately, which involves more complex code.
- Q: Will this affect the price stored in the database?
- No. This code only changes the price displayed on the front end dynamically. The original product price in the database remains unchanged.
- Q: How can I prevent caching issues with role-based pricing?
- Exclude pages with dynamic pricing from caching or use cache plugins that support dynamic content based on user roles.