How to set product discount/prices on the basis of user roles in WooCommerce?

In a previous article, We discussed about applying bulk discount for your WooCommerce products. We went through ways to apply a discount based on total price, or on number of items purchased.

Let’s consider a different scenario: You have set up your website with several different user roles and you need to offer different bulk discounts to some, or all of these user roles. For example, you want to offer bulk discounts to your employees, or business partners. Similarly, you might want to reward your most loyal customers that consistently make big purchases from your shop.

In this article, we will examine a few different ways of offering discounts based on the user role of the customer.

Table of contents
Getting ready
How to add a storewise discount
How to add a user role-based discount with a plugin
Wrapping up

Getting ready

Before doing any code modifications, it’s always a good idea to take a full backup of our site and make sure that we have FTP access or access to cPanel or Plesk or other control panel with access to a file manager.

If you wonder what is the best way to add code snippets to your site, have a look at our previous article about safely adding PHP code.

We assume that you already have WordPress with WooCommerce installed, and your store is displaying at least a few products. Also please keep in mind that, while you can simply use the default WordPress and WooCommerce user roles for this, it will work much better if you already have a system in place to apply additional user roles.

How to add a storewise discount

In the following example, we have grouped all our employees in a specific user role (“Employees”) in order to offer them a 10% discount for any purchase in our store.

First, let’s add the following snippet:

function tl_user_role_percent_discount( $cart ) {
  global $woocommerce;
  $percent = $discount = 0;

  if (is_user_logged_in() ) {
    $user = wp_get_current_user();
    $user_meta = get_userdata( $user->ID );
    $user_roles = $user_meta->roles;

    if ( in_array( 'employees', $user_roles, true ) ) {
      $percent = 10;
      $discount = $cart->get_subtotal() * $percent / 100;
      $woocommerce->cart->add_fee( 'Discount', -$discount);

    }
  }
}
add_action( 'woocommerce_cart_calculate_fees', 'tl_user_role_percent_discount', 99, 1 );

The user role slug can be updated at line 10: if ( in_array( 'employees', $user_roles, true ) ) {: Replace “employees” with your user role slug. The percentage discount at line 11: $percent = 10; can be replaced with the discount you want to offer.

Here is the cart of my user with the role “employees”, after applying the previous snippet:

Cart after applying user role based discount

How to Set Up user role-based discount with a plugin

If you only want to apply discounts to the WordPress default user roles (administrator, editor, author, contributor, subscriber) and you are not willing to mess with coding, you can use a plugin instead.

The Woocommerce Role Pricing plugin does just that with a few mouse clicks.

After installing the plugin, we can see the new option Role Pricing under WooCommerce, that presents a few options to help create a discount policy for our user groups.

The first tab, Method, is pretty self-explanatory and I will leave all the default options. Let’s move on to the second tab, Roles and configure our discount policies:

Discounts per user role screen

Here I offer a 15% discount to my Editors who buy from my shop (written as 0.15). Now, if I order a few products as a not logged in user, I will see the normal prices:

Not logged in user checkout

However, as soon as I log in with my editor user, the 15% discount has been applied:

Editor checkout

Wrapping up

User roles can have many applications in our website. Besides the security aspect, it’s a way to group users with various criteria. Grouping your customers offer them discounts depending on their group, can be a way to attract more loyal customers to your store. In this article, we examined a code snippet that allows to apply discounts for a user role. We also saw a way to do it with a plugin, if we are only using the default user roles.

We hope this article was useful. Please leave a comment if you have used any of these methods t, or if you have any questions.

Waqas

I hope you enjoy reading this blog post. If you want my team to do WooCommerce Maintenance for you, click here.

Leave a Comment

Your email address will not be published. Required fields are marked *