Untitled
webwizards
plain_text
2 months ago
6.6 kB
14
Indexable
/**
* B2BKing Rolling 12-Month Group Management System
*
* This code automatically manages customer groups based on their rolling 12-month purchase history.
* Groups can be upgraded based on spending thresholds, but can only downgrade to the lowest group
* when spending drops below €1 in the past year.
*
* Add this to your theme's functions.php file or as a custom plugin.
*/
/**
* CONFIGURATION SECTION - EDIT THESE VALUES
* Replace the group IDs with your actual B2BKing group IDs
*/
function b2bking_get_rolling_groups_config() {
return [
'groups' => [
'skupina_d' => [
'id' => 123, // Replace with actual "Veľkoobchod – Skupina D" group ID
'threshold' => 0,
'name' => 'Skupina D'
],
'skupina_c' => [
'id' => 124, // Replace with actual "Veľkoobchod – Skupina C" group ID
'threshold' => 2000,
'name' => 'Skupina C'
],
'skupina_b' => [
'id' => 125, // Replace with actual "Veľkoobchod – Skupina B" group ID
'threshold' => 5000,
'name' => 'Skupina B'
],
'skupina_a' => [
'id' => 126, // Replace with actual "Veľkoobchod – Skupina A" group ID
'threshold' => 10000,
'name' => 'Skupina A'
]
],
'downgrade_threshold' => 1, // If 12-month spending is below this amount (€1), downgrade to lowest group
'check_on_login' => true, // Check and update groups on each login
'cache_hours' => 24, // Cache the calculation for this many hours per user (set to 0 to disable caching)
];
}
/**
* Check and update customer group on login
*/
function b2bking_check_group_on_login($user_login, $user) {
$config = b2bking_get_rolling_groups_config();
if (!$config['check_on_login']) {
return;
}
// Only process for customers
if (!in_array('customer', $user->roles)) {
return;
}
// Check cache if enabled
if ($config['cache_hours'] > 0) {
$last_check = get_user_meta($user->ID, 'b2bking_last_group_check', true);
if ($last_check && (time() - $last_check < $config['cache_hours'] * 3600)) {
return; // Skip if checked recently
}
}
b2bking_update_customer_group_rolling($user->ID);
// Update cache timestamp
if ($config['cache_hours'] > 0) {
update_user_meta($user->ID, 'b2bking_last_group_check', time());
}
}
add_action('wp_login', 'b2bking_check_group_on_login', 10, 2);
/**
* Update customer group based on rolling 12-month spending
*/
function b2bking_update_customer_group_rolling($customer_id) {
$config = b2bking_get_rolling_groups_config();
// Get current group
$current_group_id = b2bking()->get_user_group($customer_id);
// Get rolling 12-month spend
$rolling_spend = b2bking_get_customer_rolling_12_month_total($customer_id);
// Determine new group based on special logic
$new_group_id = b2bking_determine_group_with_sticky_logic($rolling_spend, $current_group_id);
// Update group if changed
if ($current_group_id != $new_group_id) {
b2bking()->update_user_group($customer_id, $new_group_id);
// Log the change for debugging (optional)
b2bking_log_group_change($customer_id, $current_group_id, $new_group_id, $rolling_spend);
}
}
/**
* Determine appropriate group with the special "sticky" logic
* - Can upgrade to any higher group based on spending
* - Can only downgrade to lowest group (Skupina D) if spending < €1
* - Otherwise stays in current group
*/
function b2bking_determine_group_with_sticky_logic($spend, $current_group_id) {
$config = b2bking_get_rolling_groups_config();
// Check if spending is below downgrade threshold (€1)
if ($spend < $config['downgrade_threshold']) {
// Drop to lowest group (Skupina D)
return $config['groups']['skupina_d']['id'];
}
// Determine what group the spending qualifies for
$qualified_group_id = $config['groups']['skupina_d']['id']; // Default to lowest
// Check thresholds from highest to lowest
if ($spend >= $config['groups']['skupina_a']['threshold']) {
$qualified_group_id = $config['groups']['skupina_a']['id'];
} elseif ($spend >= $config['groups']['skupina_b']['threshold']) {
$qualified_group_id = $config['groups']['skupina_b']['id'];
} elseif ($spend >= $config['groups']['skupina_c']['threshold']) {
$qualified_group_id = $config['groups']['skupina_c']['id'];
}
// Get current group's tier level (for comparison)
$current_tier = b2bking_get_group_tier_level($current_group_id);
$qualified_tier = b2bking_get_group_tier_level($qualified_group_id);
// Apply sticky logic:
// - If qualified tier is higher, upgrade
// - If qualified tier is lower but spending >= €1, stay in current group
if ($qualified_tier > $current_tier) {
return $qualified_group_id; // Upgrade
} else {
return $current_group_id; // Stay in current group (sticky)
}
}
/**
* Get tier level of a group (higher number = higher tier)
*/
function b2bking_get_group_tier_level($group_id) {
$config = b2bking_get_rolling_groups_config();
if ($group_id == $config['groups']['skupina_a']['id']) return 4;
if ($group_id == $config['groups']['skupina_b']['id']) return 3;
if ($group_id == $config['groups']['skupina_c']['id']) return 2;
if ($group_id == $config['groups']['skupina_d']['id']) return 1;
return 0; // Unknown group
}
/**
* Get customer's completed orders total for rolling 12 months
*/
function b2bking_get_customer_rolling_12_month_total($customer_id) {
// Calculate date range (exactly 365 days ago to now)
$now = current_time('timestamp');
$one_year_ago = strtotime('-365 days', $now);
$start_date = date('Y-m-d 00:00:00', $one_year_ago);
$end_date = date('Y-m-d 23:59:59', $now);
// Query orders
$args = array(
'customer_id' => $customer_id,
'status' => array('wc-completed'), // Only completed orders
'date_created' => $start_date . '...' . $end_date,
'return' => 'ids',
'limit' => -1
);
$orders = wc_get_orders($args);
// Calculate total
$total = 0;
foreach ($orders as $order_id) {
$order = wc_get_order($order_id);
$total += $order->get_total();
}
return $total;
}
Editor is loading...
Leave a Comment