Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Show Custom Pricing Based on User Roles in WooCommerce?

Role-based pricing in WooCommerce is a good choice. It lets you customize product prices based on user roles like VIPs, wholesalers, and loyal customers. Benefits include increased customer satisfaction, loyalty, targeted marketing, and tiered pricing for membership sites. Competitive bulk pricing can help B2B and wholesale sales while giving you a competitive edge. It’s more than just pricing; it’s about creating a personalized shopping experience that encourages repeat business.

In this post, we will discuss how to enable website administrators to offer different pricing structures based on user roles for variable products.

Where to Add Custom Code in WooCommerce?

It is advisable to add the code snippets to the functions.php file of your child theme. Access the file directly from Appearance->Theme File Editor->Locating the child theme’s functions.php from the right sidebar. You can also access it from your theme’s directory file. Insert the following code snippet in functions.php. The alternative & easy option is to install & activate the Code Snippets plugin. You can then add the code as a new snippet via the plugin.

Preliminary Steps

You need to create new users & assign separate roles to them before implementing the code snippets. Follow the given steps below:-

  • Go to the Users page and click the Add New button.
  • Enter the username and other required information.
  • Under the Role section, select the roles you want to assign to the user.
  • Click the Add New User button to save your changes to your WordPress user profile.
Custom Pricing Based on User Roles in WooCommerce?

Solution: Displaying Custom Pricing Based on User Roles

Imagine you have an online clothing store with user roles, including regular customers and wholesaler members. The following code snippets adjust product prices for different customers based on their roles. For instance, when a customer logs in as a regular shopper, they will see the standard prices, and when a user logs in as a wholesaler, they will able to see the discount product price.

add_filter( 'woocommerce_get_price', 'ts_custom_price', 10, 2 );

function ts_custom_price( $price, $product ) {

    // Get the current user's role.
    $user_role = wp_get_current_user()->roles[0];

    // Set different prices for different user roles.
    switch ( $user_role ) {
        case 'wholesaler':
            $price = $price * 0.9; // Give wholesaler a 10% discount.
            break;
        case 'customer':
            $price = $price; // Keep the regular price for customers.
            break;
        default:
            $price = $price; // Keep the regular price for other user roles.
    }

    // Return the new price.
    return $price;
}

Output

Use Case 1: Displaying the Product Price for the WholeSaler

When a user logs in as a wholesaler role, they will see the 10% discount on the product price as shown below.

Displaying the Product Price for the WholeSaler

Use Case 2: Displaying the Product Price for the Customer

When a user logs in as a customer role, they will see the regular price of the product as shown below.

Displaying the Product Price for the WholeSaler
How to Show Custom Pricing Based on User Roles in WooCommerce? - Tyche Softwares

Easily Set Fees & Discounts For Payment Gateways

Finding it difficult to manage all payment gateway fees and adjusting your product pricing accordingly?

The Payment Gateway Based Fees & Discounts plugin is designed to set up fees & discounts for each of your payment modes and simplify your payment process.

Code Explanation

Here is the explanation of the code

1. Hooking into WooCommerce Filter:

This line adds a filter to the WooCommerce hook woocommerce_get_price, which allows us to modify the product price using the ‘custom_price’ function. The ’10’ is the priority of the filter, and ‘2’ indicates that the function accepts two arguments.

2. Custom Price Function:

This function accepts two arguments: $price (the product’s original price) and $product (the WooCommerce product object).

3. Get User Role:

This line retrieves the current user’s role using wp_get_current_user() and stores it in the variable $user_role. The [0] index is used to get the primary role, assuming the user has only one role.

4. Set Different Prices for User Roles:

The code uses a switch statement to apply different pricing rules based on the user’s role. It evaluates the user’s role and adjusts the product price accordingly.

  • WholeSaler:
    • If the user is a ‘wholesaler,’ a 10% discount is applied by multiplying the original price by 0.9.
  • Customer:
    • If the user is a ‘customer,’ the price remains unchanged.
  • Default:
    • For all other user roles not specified in the switch statement, the price remains unchanged.

5. Return the New Price:

$price is updated based on the user’s role, and the modified price is returned to WooCommerce.

Conclusion

The above code snippets help you with the implementation of adjusting product prices based on the user’s role. However, additional pricing strategies are well met via a plugin named Product Prices By User Roles for WooCommerce which gives you even more control over pricing and user roles.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x