Hey there! Today I want to share a project I completed as part of a technical interview with a contractor from Upwork. The task was to create a role-based discount system for a beverage shop running on WooCommerce. This kind of pricing system is super valuable for businesses that offer different prices to different customer types – think wholesale buyers, VIP customers, or in this case: regular customers, special customers, resellers, and super resellers.

I’ll walk you through my implementation process, from setting up the environment to testing the final product. Let’s dive in!
Development Environment Setup
Before jumping into code, I needed to set up a proper development environment. Here’s what I did:
- Installed LocalWP for local WordPress development – this tool makes it super easy to create isolated WordPress installations
- Set up Storefront theme and created a child theme – Storefront is WooCommerce’s official theme, making it perfect for this project
- Configured Prepros as my build tool with live reload capabilities – this saved tons of time during development
With the environment ready, I could start developing the actual discount system.
Development Process
Registering Custom User Roles in WordPress
The first step was creating custom user roles to differentiate between customer types. I needed three new roles beyond the default “Customer” role:
- Special Customer
- Reseller
- Super Reseller
Here’s a snippet of how I registered these custom roles:
// Custom user roles registration
function register_custom_roles() {
// Special Customer role
add_role('special_customer', 'Special Customer', array(
'read' => true,
// Other capabilities...
));
// Reseller role
add_role('reseller', 'Reseller', array(
'read' => true,
// Other capabilities...
));
// Super Reseller role
add_role('super_reseller', 'Super Reseller', array(
'read' => true,
// Other capabilities...
));
}

Implementing Discounts for Simple Products
With the roles in place, I needed to add custom discount fields to the product editor for simple products. These fields would allow the shop admin to set discount percentages for each customer type.
I added three discount fields to the “Simple Product” editor panel:
- Special Customer Discount (%)
- Reseller Discount (%)
- Super Reseller Discount (%)
Additionally, I included read-only fields that would show the calculated prices after applying the discounts. Here’s part of the implementation:
function add_simple_product_discount_fields() {
global $post;
// Get current discount values
$special_customer_discount = get_post_meta($post->ID, '_special_customer_discount', true);
$reseller_discount = get_post_meta($post->ID, '_reseller_discount', true);
// More code...
echo '<div class="options_group show_if_simple">';
// Special Customer Discount Fields
echo '<h4>Set Price for Special Customers</h4>';
woocommerce_wp_text_input(
array(
'id' => '_special_customer_discount',
'label' => 'Discount (%)',
'desc_tip' => true,
'description' => 'Enter the discount percentage for Special Customers.',
// More parameters...
)
);
// More discount fields for other roles...
}
I also implemented JavaScript to dynamically calculate and display the discounted prices when the admin changes the discount percentages.

Implementing Discounts for Variable Products
For variable products (like coffee with different types and sizes), I applied a similar approach but had to account for each variation. This was more complex, as I needed to add discount fields to each product variation and provide real-time price calculations.
Here’s a glimpse of the implementation:
function add_variation_discount_fields($loop, $variation_data, $variation) {
// Get current discount values
$special_customer_discount = get_post_meta($variation->ID, '_special_customer_discount', true);
$reseller_discount = get_post_meta($variation->ID, '_reseller_discount', true);
// More code...
// Output custom fields
echo '<div class="role-based-pricing">';
echo '<p><strong>Set Discount Percentages for User Roles</strong></p>';
// Special Customer Discount
echo '<div class="discount-field-group">';
// Fields implementation...
echo '</div>';
// More discount fields for other roles...
}
Setting Default Discounts Based on Product Type
To make the admin’s life easier, I implemented logic to set default discount values based on product types. For example, Cinnamon Tea would get a 15% reseller discount, while Arabica coffee in a big box would get a 32% super reseller discount.
function set_default_discounts_for_product($post_id) {
// Check if this is a new product
if (get_post_meta($post_id, '_default_discounts_set', true) === 'yes') {
return;
}
// Get product object
$product = wc_get_product($post_id);
if (!$product) {
return;
}
// Get product type and name
$product_type = $product->get_type();
$product_name = $product->get_name();
// Default discounts based on product name/type
$special_customer_discount = 10; // 10% for all products
$reseller_discount = 20; // 20% default for resellers
$super_reseller_discount = 25; // 25% default for super resellers
// Set specific discounts based on product
// More conditional logic for different products...
}
Displaying Discount Information in the Frontend
Now that the admin could set discounts, users needed to see their special pricing. I added a feature to display the applied discount percentage and the final price on the product page, but only for users with special roles.
function display_discount_info() {
global $product;
// Get current user
$user = wp_get_current_user();
$user_role = $user->roles[0] ?? 'customer';
// If user is not logged in or is a regular customer, don't show discount info
if (!is_user_logged_in() || $user_role === 'customer') {
return;
}
// For variable products, handle differently with JavaScript
if ($product->is_type('variable')) {
// JavaScript implementation for variations...
} else {
// For simple products, display static discount info
// Implementation details...
}
}
Implementing AJAX for Dynamic Price Calculations
For variable products, I needed to dynamically update the discount information when a user selected different variations. I implemented an AJAX solution to fetch the correct discount information whenever a variation was selected.
// Client-side JavaScript
jQuery(document).ready(function($) {
// Function to update discount info
function updateDiscountInfo(variation) {
// AJAX call to get the discount for this variation
$.ajax({
url: admin_ajax_url,
type: 'POST',
data: {
'action': 'get_variation_discount',
'variation_id': variation.variation_id,
'user_role': userRole
},
success: function(response) {
// Update the display with discount info
// More code...
}
});
}
// Listen for variation changes
$('.variations_form').on('found_variation', function(event, variation) {
updateDiscountInfo(variation);
});
});
// Server-side AJAX handler
function get_variation_discount() {
$variation_id = isset($_POST['variation_id']) ? intval($_POST['variation_id']) : 0;
$user_role = isset($_POST['user_role']) ? sanitize_text_field($_POST['user_role']) : '';
// Get discount based on user role
$discount = 0;
if ($user_role === 'special_customer') {
$discount = get_post_meta($variation_id, '_special_customer_discount', true);
} elseif ($user_role === 'reseller') {
$discount = get_post_meta($variation_id, '_reseller_discount', true);
} elseif ($user_role === 'super_reseller') {
$discount = get_post_meta($variation_id, '_super_reseller_discount', true);
}
wp_send_json_success(array(
'discount' => $discount
));
}

Testing Across Different User Roles
To verify that everything was working correctly, I tested the system with different user roles. I created test accounts for each role and verified that:
- Regular customers saw the normal prices

- Special customers saw the “Special Customer Discount” and reduced prices

- Resellers saw their specific discount and prices

- Super resellers saw their discount and the lowest prices
You can test this system yourself at https://case-study.rizwanaritonang.com/shop-1 using these credentials:
- Role: Customer
- username: JustCustomer
- password: LJneZd8YDd8J)6BwD$A#&13F
- Role: Special Customer
- username: TestSpecialCustomer
- password: Mw@okNZoNdlOLFtbgvvv3&vt
- Role: Reseller Account
- username: ResellerAccount
- password: LeRLEgvayJVcs*m&sjDSJSu%
Conclusion
Implementing a role-based discount system in WooCommerce was a challenging but rewarding project. The key takeaways from this implementation are:
- Custom user roles are essential for differentiating between customer types
- Custom meta fields provide flexible discount options for administrators
- JavaScript calculations enhance the UX for both admin and customers
- AJAX is crucial for dynamic updates with variable products
- Default values can save tremendous time for store administrators
This solution allows beverage shop owners to offer tiered pricing to different customer types without needing separate stores or complex plugins. It’s a flexible and maintainable approach that can be extended for other e-commerce needs.
Feel free to check out the demo and let me know if you have any questions or suggestions for improvement!