Show User Group / Level and Total Spent on the My Account Page

Let's take a look at how we can show B2B customers their current group and other information such as total spent, discount levels, etc. directly on their my account dashboard page.

There is currently no setting for this, but it can be achieved by adding PHP code snippets to your site.

Example 1) Show the Customer Group #

To achieve this, add the following snippet to your site:

add_action('woocommerce_account_dashboard', function(){
    $user_id = get_current_user_id();
    $is_b2b = get_user_meta($user_id,'b2bking_b2buser', true);
    if ($is_b2b === 'yes'){
        $group = get_user_meta($user_id,'b2bking_customergroup', true);
        $group_name = get_the_title($group);
        ?>
        Account Type: <?php echo $group_name; ?>
        <?php
    }
});

After adding this snippet, B2B users will see their current group here on the my account page:

Example 2) Show the Customer Group and Total Spent #

Let's add the following snippet to the site:

add_action('woocommerce_account_dashboard', function(){
    $user_id = get_current_user_id();
    $is_b2b = get_user_meta($user_id,'b2bking_b2buser', true);
    if ($is_b2b === 'yes'){
        $group = get_user_meta($user_id,'b2bking_customergroup', true);
        $group_name = get_the_title($group);

        $customer = new WC_Customer($user_id);
        $total_spent = $customer->get_total_spent();
        ?>
        Account Type: <?php echo '<strong>'.$group_name.'</strong><br>'; ?>
        Total Spent: <?php echo '<strong>'.wc_price($total_spent).'</strong><br><br>'; ?>

        Group Levels: <br>
        Level 1 - 10% Discount | < $2,000 Spent<br>
        Level 2 - 15% Discount | $2,000 - $10,000 Spent<br>
        Level 3 - 20% Discount | > $10,000 Spent<br>
        <?php
    }
});

This snippet shows the current group and the total spent, as well as some example discount levels below to help incentivise the customer:

The above can of course be customised.

Example 3: Show Group, Total Spent, and Loading Bar to Next Level #

If we have discount levels and a total spent, it might be nice to have a loading bar showing the customer their current progress. This is a bit complex, but we have a snippet for it:

add_action('wp_head', function(){
	?>
	<style type="text/css">
		.wrapper {
			width: 500px;
		}
		
		.progress-bar {
			width: 100%;
			background-color: #e0e0e0;
			padding: 3px;
			border-radius: 3px;
			box-shadow: inset 0 1px 3px rgba(0, 0, 0, .2);
			color: white;
		}
		
		.progress-bar-fill {
			display: flex;
			justify-content: center;
			align-items: center;
			height: 22px;
			background-color: #659cef;
			border-radius: 3px;
			transition: width 500ms ease-in-out;
		}
	</style>
	<?php
});


add_action('woocommerce_account_dashboard', function(){
    $user_id = get_current_user_id();
    $is_b2b = get_user_meta($user_id,'b2bking_b2buser', true);
    if ($is_b2b === 'yes'){
        $group = get_user_meta($user_id,'b2bking_customergroup', true);
        $group_name = get_the_title($group);

        $customer = new WC_Customer($user_id);
        $total_spent = $customer->get_total_spent();

        $discount_levels = array(
        	1 => 0,
        	2 => 500,
        	3 => 1500,
        	4 => 5000,
        );

        ?>
        Account Type: <?php echo '<strong>'.$group_name.'</strong><br>'; ?>
        Total Spent: <?php echo '<strong>'.wc_price($total_spent).'</strong><br>'; ?>

        <?php

        $customer_level = 1;
        foreach ($discount_levels as $level => $threshold){
        	if ($total_spent > $threshold){
        		$customer_level = $level;
        	}	
        }

        $next_level = intval($customer_level) + 1; // get the next level

        if (!isset($discount_levels[$next_level])){
        	$progress = 100;
        } else {
        	$progress = $total_spent / $discount_levels[$next_level] * 100; // get the progress to the next level in percentage
        }

        ?>
        <br>
        Progress to the next level:
        <div class="wrapper">
			<div class="progress-bar">
				<span class="progress-bar-fill" style="width: <?php echo $progress; ?>%;"><?php echo round($progress, 1).'%';?></span>
			</div>
		</div>
		<br>

		Group Levels: <br><br>

		<?php 
		foreach ($discount_levels as $level => $threshold){
			if ($level === $customer_level) { echo '<strong>'; } // bold the customer's current level

			echo 'Level '.$level.' - '.wc_price($threshold).' spent<br>';

			if ($level === $customer_level) { echo '</strong>'; } // bold the customer's current level

		}

    }
});

In the snippet above, all that needs to be changed is basically the $discount_levels variable - there you can configure any number of levels and their thresholds.

Here's what this looks like:

Powered by BetterDocs