Untitled
unknown
plain_text
a year ago
4.4 kB
8
Indexable
// Define groups and their thresholds
$agent_groups = array(
array(
'name' => 'Group A',
'id' => '123',
'threshold' => 0 // Base group
),
array(
'name' => 'Group B',
'id' => '234',
'threshold' => 10000
),
array(
'name' => 'Group C',
'id' => '345',
'threshold' => 25000
)
);
function calculate_agent_monthly_sales($agent_id) {
$current_month = date('m');
$current_year = date('Y');
$earnings = get_posts(array(
'post_type' => 'salesking_earning',
'numberposts' => -1,
'post_status' => 'any',
'fields' => 'ids',
'meta_key' => 'agent_id',
'meta_value' => $agent_id,
));
$total_agent_commissions = 0;
foreach ($earnings as $earning_id) {
$order_id = get_post_meta($earning_id, 'order_id', true);
$orderobj = wc_get_order($order_id);
if ($orderobj !== false) {
$order_date = $orderobj->get_date_created();
// Check if order is from current month
if ($order_date->format('m') == $current_month && $order_date->format('Y') == $current_year) {
$earnings_total = get_post_meta($earning_id, 'salesking_commission_total', true);
if (!empty($earnings_total) && floatval($earnings_total) !== 0) {
$status = $orderobj->get_status();
// Include processing and on-hold status
if (in_array($status, apply_filters('salesking_earning_counted_statuses',
array('completed', 'processing', 'on-hold')))) {
$total_agent_commissions += $earnings_total;
}
}
}
}
}
return $total_agent_commissions;
}
function get_appropriate_group($sales_amount) {
global $agent_groups;
// Sort groups by threshold in descending order
usort($agent_groups, function($a, $b) {
return $b['threshold'] - $a['threshold'];
});
// Find appropriate group
foreach ($agent_groups as $group) {
if ($sales_amount >= $group['threshold']) {
return $group['id'];
}
}
// Return base group if no threshold met
return $agent_groups[count($agent_groups) - 1]['id'];
}
/**
* Update agent groups based on monthly sales
*/
function update_agent_groups() {
$users = get_users(array(
'meta_key' => 'salesking_group',
'meta_value' => 'none',
'meta_compare' => '!='
));
foreach ($users as $user) {
$monthly_sales = calculate_agent_monthly_sales($user->ID);
$appropriate_group = get_appropriate_group($monthly_sales);
// Update user's group if different
$current_group = get_user_meta($user->ID, 'salesking_group', true);
if ($current_group !== $appropriate_group) {
update_user_meta($user->ID, 'salesking_group', $appropriate_group);
}
}
}
/**
* Reset all agents to base group at month start
*/
function reset_agent_groups() {
global $agent_groups;
// Get base group (lowest threshold)
usort($agent_groups, function($a, $b) {
return $a['threshold'] - $b['threshold'];
});
$base_group = $agent_groups[0]['id'];
$users = get_users(array(
'meta_key' => 'salesking_group',
'meta_value' => 'none',
'meta_compare' => '!='
));
foreach ($users as $user) {
update_user_meta($user->ID, 'salesking_group', $base_group);
}
}
// Hook into WP Cron for daily checks
add_action('wp_scheduled_events', 'check_and_update_groups');
function check_and_update_groups() {
// If it's the first day of the month, reset groups
if (date('j') === '1') {
reset_agent_groups();
}
// Update groups based on current sales
update_agent_groups();
}
add_action('woocommerce_order_status_changed', 'trigger_group_update', 10, 4);
function trigger_group_update($order_id, $old_status, $new_status, $order) {
// Update groups when order status changes to a counted status
$counted_statuses = apply_filters('salesking_earning_counted_statuses',
array('completed', 'processing', 'on-hold'));
if (in_array($new_status, $counted_statuses)) {
update_agent_groups();
}
}Editor is loading...
Leave a Comment