Untitled

webwizards avatar
webwizards
plain_text
02/17/2026 11:56 PM
3.7 KB
13
Indexable
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(){

    // Bail early if WooCommerce isn't available
    if ( ! class_exists('WC_Customer') ) {
        return;
    }

    $user_id = get_current_user_id();

    if ( ! $user_id ) {
        return;
    }

    $is_b2b = get_user_meta($user_id, 'b2bking_b2buser', true);

    if ( $is_b2b !== 'yes' ) {
        return;
    }

    $group = get_user_meta($user_id, 'b2bking_customergroup', true);
    $group_name = $group ? get_the_title($group) : 'N/A';

    try {
        $customer = new WC_Customer($user_id);
        $total_spent = (float) $customer->get_total_spent();
    } catch ( Exception $e ) {
        $total_spent = 0.0;
    }

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

    ?>
    Account Type: <strong><?php echo esc_html($group_name); ?></strong><br>
    Total Spent: <strong><?php echo 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 = $customer_level + 1;

    if ( ! isset($discount_levels[$next_level]) ) {
        $progress = 100;
    } else {
        $next_threshold = (float) $discount_levels[$next_level];
        $progress = $next_threshold > 0 ? ( $total_spent / $next_threshold * 100 ) : 0;
    }

    // Clamp progress between 0 and 100
    $progress = max(0, min(100, $progress));

    ?>
    <br>
    Progress to the next level:
    <div class="wrapper">
        <div class="progress-bar">
            <span class="progress-bar-fill" style="width: <?php echo esc_attr($progress); ?>%;">
                <?php echo esc_html(round($progress, 1) . '%'); ?>
            </span>
        </div>
    </div>
    <br>
    Group Levels:
    <br><br>
    <?php
    foreach ( $discount_levels as $level => $threshold ) {
        if ( (int) $level === (int) $customer_level ) {
            echo '<strong>';
        }
        echo 'Level ' . intval($level) . ' - ' . wc_price($threshold) . ' spent<br>';
        if ( (int) $level === (int) $customer_level ) {
            echo '</strong>';
        }
    }
});
Editor is loading...
Leave a Comment