Untitled
unknown
plain_text
6 months ago
5.6 kB
8
Indexable
function get_customer_completed_orders_total2($customer_id, $period = 'current') {
// Define date parameters based on period
$now = current_time('timestamp');
// Set up the query arguments with common parameters
$args = array(
'customer_id' => $customer_id,
'status' => array('wc-completed'),
'return' => 'ids',
'limit' => -1
);
if ($period === 'current') {
// Current month: from 1st of current month to now
$start_date = date('Y-m-01 00:00:00', $now);
$end_date = date('Y-m-d H:i:s', $now);
$args['date_created'] = $start_date . '...' . $end_date;
} elseif ($period === 'previous') {
// Previous month: from 1st to last day of previous month
$first_day_previous_month = date('Y-m-01 00:00:00', strtotime('-1 month', $now));
$last_day_previous_month = date('Y-m-t 23:59:59', strtotime('-1 month', $now));
$args['date_created'] = $first_day_previous_month . '...' . $last_day_previous_month;
} // For 'total' period, we don't add date constraints, so it will get all completed orders
// Get the orders
$orders = wc_get_orders($args);
// Calculate the total
$total = 0;
foreach ($orders as $order_id) {
$order = wc_get_order($order_id);
$total += $order->get_total();
}
return $total;
}
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);
// Get monthly spent
$monthly_spent = get_customer_completed_orders_total2($user_id, 'current');
// Define the new levels with descriptive names and thresholds in euros
$discount_levels = array(
'basic' => 0, // Less than 200€
'standard' => 200, // 200€ to 999.99€
'plus' => 1000, // 1,000€ to 2,999.99€
'premium' => 3000, // 3,000€ and above
);
// Level descriptions for display
$level_descriptions = array(
'basic' => 'Basic',
'standard' => 'Standard - From 200€ to 999.99€ per month',
'plus' => 'Plus - From 1,000€ to 2,999.99€ per month',
'premium' => 'Premium - 3,000€ and above per month',
);
?>
Account Type: <?php echo '<strong>'.$group_name.'</strong><br>'; ?>
Monthly Spent: <?php echo '<strong>'.wc_price($monthly_spent).'</strong><br>'; ?>
<?php
// Determine customer level based on monthly spent
$customer_level = 'basic';
foreach ($discount_levels as $level => $threshold) {
if ($monthly_spent >= $threshold) {
$customer_level = $level;
}
}
// Get next level
$levels = array_keys($discount_levels);
$current_index = array_search($customer_level, $levels);
$next_index = $current_index + 1;
// Calculate progress to next level
if ($next_index >= count($levels)) {
// Already at highest level
$progress = 100;
$next_level_name = 'Maximum Level Reached';
} else {
$next_level = $levels[$next_index];
$next_threshold = $discount_levels[$next_level];
// Modified progress calculation - based directly on the next threshold
if ($customer_level == 'basic' && $monthly_spent < 200) {
// For Basic level, calculate progress toward Standard (200€)
$progress = min(($monthly_spent / 200) * 100, 100);
} else {
// For other levels, calculate progress toward the next threshold
// For example, with $500 spend for Standard level, it should show 50% of the way to Plus ($1000)
if ($customer_level == 'standard') {
$progress = min(($monthly_spent / 1000) * 100, 100);
} else if ($customer_level == 'plus') {
$progress = min(($monthly_spent / 3000) * 100, 100);
}
}
$next_level_name = ucfirst($next_level);
}
?>
<br>
Current Level: <strong><?php echo ucfirst($customer_level); ?></strong>
<br><br>
Progress to <?php echo $next_level_name; ?>:
<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>
Available Levels:
<br><br>
<?php
foreach ($discount_levels as $level => $threshold) {
if ($level === $customer_level) {
echo '<strong>';
}
echo $level_descriptions[$level] . '<br>';
if ($level === $customer_level) {
echo '</strong>';
}
}
}
});Editor is loading...
Leave a Comment