Untitled
unknown
plain_text
7 months ago
7.0 kB
3
Indexable
/**
* CONFIGURATION - EDIT THESE VALUES
*/
// Configure your B2BKing group IDs
$b2bking_group_ids = array(
'vip' => 124, // Replace with actual VIP group ID
'standard' => 123, // Replace with actual Standard group ID
'basic' => 125 // Replace with actual Basic group ID
);
// Configure UPGRADE rules (move customer UP to better group)
$b2bking_upgrade_rules = array(
// FROM Basic group, if they spend >= 25000, upgrade to Standard
array(
'from_group' => 'basic',
'to_group' => 'standard',
'min_spend' => 25000
),
// FROM Standard group, if they spend >= 50000, upgrade to VIP
array(
'from_group' => 'standard',
'to_group' => 'vip',
'min_spend' => 50000
),
);
// Configure DOWNGRADE rules (move customer DOWN to lower group)
$b2bking_downgrade_rules = array(
// FROM VIP group, if they spend < 40000, downgrade to Standard
array(
'from_group' => 'vip',
'to_group' => 'standard',
'min_spend' => 40000
),
// FROM Standard group, if they spend < 20000, downgrade to Basic
array(
'from_group' => 'standard',
'to_group' => 'basic',
'min_spend' => 20000
),
);
/**
* CODE BELOW - No need to edit unless you want to customize functionality
*/
// Schedule the quarterly check event
function b2bking_schedule_quarterly_checks() {
// Schedule the event if it's not already scheduled
if (!wp_next_scheduled('b2bking_quarterly_group_rules_check')) {
// Set to run at midnight on the 1st day of each quarter
$next_quarter_dates = array(
mktime(0, 0, 0, 1, 1, date('Y')), // January 1st
mktime(0, 0, 0, 4, 1, date('Y')), // April 1st
mktime(0, 0, 0, 7, 1, date('Y')), // July 1st
mktime(0, 0, 0, 10, 1, date('Y')), // October 1st
);
// Find the next upcoming quarter date
$now = time();
foreach ($next_quarter_dates as $date) {
if ($date > $now) {
wp_schedule_event($date, 'quarterly', 'b2bking_quarterly_group_rules_check');
break;
}
}
// If we've passed all quarter dates for this year, schedule for next year's January 1st
if (!wp_next_scheduled('b2bking_quarterly_group_rules_check')) {
wp_schedule_event(
mktime(0, 0, 0, 1, 1, date('Y') + 1),
'quarterly',
'b2bking_quarterly_group_rules_check'
);
}
}
}
add_action('init', 'b2bking_schedule_quarterly_checks');
// Register the quarterly interval for wp_schedule_event
function b2bking_add_quarterly_schedule($schedules) {
$schedules['quarterly'] = array(
'interval' => 7776000, // 90 days in seconds (approximate quarter)
'display' => __('Quarterly')
);
return $schedules;
}
add_filter('cron_schedules', 'b2bking_add_quarterly_schedule');
// Function to process all users and check their quarterly order totals
function b2bking_process_quarterly_group_rules() {
// Get all users with the customer role
$customer_users = get_users(array('role' => 'customer'));
// Loop through each customer
foreach ($customer_users as $user) {
b2bking_check_quarterly_rules($user->ID);
}
}
add_action('b2bking_quarterly_group_rules_check', 'b2bking_process_quarterly_group_rules');
// Function to check if a user meets the quarterly order total criteria
function b2bking_check_quarterly_rules($user_id) {
global $b2bking_group_ids, $b2bking_upgrade_rules, $b2bking_downgrade_rules;
// Get current user group
$current_group_id = get_user_meta($user_id, 'b2bking_customergroup', true);
// Skip if user doesn't have a B2BKing group
if (empty($current_group_id)) {
return;
}
// Find current group name based on ID
$current_group_name = array_search($current_group_id, $b2bking_group_ids);
// Skip if user is not in one of our defined groups
if ($current_group_name === false) {
return;
}
// Set date range for the previous calendar quarter
$current_month = date('n');
$current_year = date('Y');
// Determine which quarter we just completed
if ($current_month >= 1 && $current_month <= 3) {
// We're in Q1, so check Q4 of previous year
$quarter_start_month = 10;
$quarter_start_year = $current_year - 1;
} else {
// Check the previous quarter of current year
$quarter_start_month = floor(($current_month - 1) / 3) * 3 - 2;
$quarter_start_year = $current_year;
}
$quarter_end_month = $quarter_start_month + 2;
$quarter_end_year = $quarter_start_year;
$start_date = date('Y-m-d H:i:s', mktime(0, 0, 0, $quarter_start_month, 1, $quarter_start_year));
$end_date = date('Y-m-d H:i:s', mktime(23, 59, 59, $quarter_end_month, date('t', mktime(0, 0, 0, $quarter_end_month, 1, $quarter_end_year)), $quarter_end_year));
// Calculate order total for the quarter
$quarterly_total = b2bking_get_user_order_total($user_id, $start_date, $end_date);
// First check if any upgrade rule applies
$new_group_name = null;
// Check upgrade rules
foreach ($b2bking_upgrade_rules as $rule) {
if ($rule['from_group'] === $current_group_name && $quarterly_total >= $rule['min_spend']) {
$new_group_name = $rule['to_group'];
break;
}
}
// If no upgrade happened, check if any downgrade rule applies
if ($new_group_name === null) {
foreach ($b2bking_downgrade_rules as $rule) {
if ($rule['from_group'] === $current_group_name && $quarterly_total < $rule['min_spend']) {
$new_group_name = $rule['to_group'];
break;
}
}
}
// If a group change is needed, update the user's group
if ($new_group_name !== null && isset($b2bking_group_ids[$new_group_name])) {
$new_group_id = $b2bking_group_ids[$new_group_name];
// Update the user's group
update_user_meta($user_id, 'b2bking_customergroup', $new_group_id);
}
}
// Function to calculate a user's order total for a specific date range
function b2bking_get_user_order_total($user_id, $start_date, $end_date) {
// Get all orders for this user in the date range
$args = array(
'customer_id' => $user_id,
'date_created' => $start_date . '...' . $end_date,
'status' => array('wc-completed', 'wc-processing'), // Only count completed and processing orders
'limit' => -1, // Get all orders
);
$orders = wc_get_orders($args);
$total = 0;
// Sum the order totals
foreach ($orders as $order) {
$total += $order->get_total();
}
return $total;
}Editor is loading...
Leave a Comment