WooCommerce: Switch User Role If Customer Spent More Than $

Many businesses allow their customers to unlock benefits and discounts once a certain spend amount is reached.

Think about a “VIP club“, where members can get an exclusive 20% discount once they reach $1,000 worth of store purchases.

Let’s also say that, when the threshold is reached, you want to promote users from the “customer” role to a custom “VIP customer” role, so that it’s easier to segment your email marketing and overall targeting.

Well, the snippet below will allow you to switch user role once a certain spend is reached. The function will trigger when a customer places an order, so that we can calculate the total spent, and possibly switch their role. Enjoy!

Thanks to the snippet below, customers will automatically switch to the “VIP customer” role (previously created with the plugin mentioned below) once they reach a given total spent threshold.

PHP Snippet: Maybe Switch User Role When Customer Spends More Than $$$ (Lifetime)

Note: of course you first need to create your custom user role (‘vip-customer’ in the example below, but you can name it as you wish as long as you change the code accordingly). You can use the free User Role Editor WordPress plugin if you wish.

/**
 * @snippet       Maybe Switch User Role Upon WooCommerce Order
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */

add_action( 'woocommerce_order_status_changed', 'bbloomer_maybe_trigger_switch_user_role' );

function bbloomer_maybe_trigger_switch_user_role( $order_id ) {
	$order = wc_get_order( $order_id );
	$user_id = $order->get_user_id();
	$order_status = $order->get_status();
	$switch_already_done = $order->get_meta( '_bb_role_switched' );
	if ( ! $switch_already_done && $order->has_status( wc_get_is_paid_statuses() ) && wc_user_has_role( $user_id, 'customer' ) ) {
		bbloomer_customer_maybe_upgrade_to_vip( $user_id );
      $order->update_meta_data( '_bb_role_switched', 'true' );
		$order->save();
	}
}

function bbloomer_customer_maybe_upgrade_to_vip( $user_id ) {
   if ( wc_get_customer_total_spent( $user_id ) > 999.99 ) { // THRESHOLD
      $user = new WP_User( $user_id );
		$user->add_role( 'vip-customer' );
		$user->remove_role( 'customer' );
	}
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Disable Payment Gateway for Specific User Role
    You may want to disable payment gateways depending on the logged in user role. For example, you may want to disable PayPal for user role “subscriber” or enable a specific gateway for user role “customer”. All you need is to paste the following code in your functions.php or to install a super simple plugin. Enjoy!
  • WooCommerce: Login Redirect by User Role @ My Account
    There are times when you don’t want customers to login and be redirected to the default “My Account” dashboard. Maybe because you have a membership site and you want them to go to a custom “Welcome” page, or maybe you wish to send them straight to their “My Account” > “Downloads” subsection. No matter the […]
  • WooCommerce B2B: How to Set Up a Wholesale Store
    The ecommerce sector is seeing incredible growth, year after year, with no foreseeable end in sight. The same is true for B2B ecommerce, yet there aren’t many good platform choices available for small-to-medium businesses that want to sell wholesale. There are several SaaS solutions on the market, but these are costly, closed-source, and mostly oriented […]
  • WooCommerce: Change User Role for New Customers
    If you don’t want to assign the WooCommerce user role “customer” to new… customers, there is simple PHP that can be added to your functions.php to achieve this. Enjoy!
  • Tax display by customer role?
    In a WooCommerce store, I have two roles: Customer and Professional. In the WooCommerce options, I have selected to display prices in the cart and checkout with taxes included. Now, I need for the Professional role to have the opposite setting, that in the cart and checkout the prices are displayed without taxes included. Is there a way to […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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