Untitled
unknown
plain_text
a year ago
2.6 kB
9
Indexable
function get_commission($rule_applied_id, $agent_id = 'all', $time_period = 'all') {
$args = array(
'post_type' => 'salesking_earning',
'posts_per_page' => -1,
'meta_query' => array(),
);
// Add agent_id to meta query if specified
if ($agent_id !== 'all') {
$args['meta_query'][] = array(
'key' => 'agent_id',
'value' => $agent_id,
'compare' => '='
);
}
// Add date range to query based on time_period
$date_query = array();
switch ($time_period) {
case 'current_month':
$date_query = array(
'year' => date('Y'),
'month' => date('m'),
);
break;
case 'last_month':
$date_query = array(
'year' => date('Y', strtotime('-1 month')),
'month' => date('m', strtotime('-1 month')),
);
break;
// Add more cases for different time periods as needed
}
if (!empty($date_query)) {
$args['date_query'] = array($date_query);
}
$query = new WP_Query($args);
$total_commission = 0;
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
// Get and unserialize the rules_apply_log
$rules_apply_log = maybe_unserialize(get_post_meta($post_id, 'rules_apply_log', true));
// Check if the specified rule is applied
$rule_applied = false;
if (is_array($rules_apply_log) && !empty($rules_apply_log)) {
foreach ($rules_apply_log as $log_entry) {
if (isset($log_entry[2]) && strpos($log_entry[2], 'Rules that apply:') !== false) {
$applied_rules = maybe_unserialize(substr($log_entry[2], strpos($log_entry[2], 'a:')));
if (is_array($applied_rules) && in_array($rule_applied_id, $applied_rules)) {
$rule_applied = true;
break;
}
}
}
}
// If the rule is applied, add the commission to the total
if ($rule_applied) {
$commission = get_post_meta($post_id, 'salesking_commission_total', true);
$total_commission += floatval($commission);
}
}
}
wp_reset_postdata();
return $total_commission;
}
Editor is loading...
Leave a Comment