function cas_get_month_stats() {
global $wpdb;
$referrals = 0;
$sales = 0;
$discount = 0;
$commission = 0;
// Define date range for the current month
$start_date = date('Y-m-01 00:00:00');
$end_date = date('Y-m-t 23:59:59');
$orders = wc_get_orders([
'limit' => -1,
'status' => ['processing', 'completed'],
'date_created' => $start_date . '...' . $end_date,
]);
if (!empty($orders)) {
foreach ($orders as $order) {
$coupons = $order->get_coupon_codes();
if (empty($coupons)) continue;
$referrals++;
// Use subtotal to match most reporting tools
$order_subtotal = (float) $order->get_subtotal();
$order_discount = (float) $order->get_discount_total();
$sales += $order_subtotal;
$discount += $order_discount;
// Get Commission Percent
$coupon_code = $coupons[0];
$coupon = new WC_Coupon($coupon_code);
$c_id = $coupon->get_id();
$commission_percent = get_post_meta($c_id, 'wcusage_commission_percent', true) ?:
get_post_meta($c_id, 'wcu_commission_percent', true) ?: 100;
// CHECK: Is commission based on Sales or Discount?
// If based on Sales: ($order_subtotal * $commission_percent) / 100
$commission += ($order_discount * (float)$commission_percent) / 100;
}
}
// Clicks logic remains the same
$click_table = $wpdb->prefix . 'wcusage_clicks';
$clicks = 0;
if ($wpdb->get_var("SHOW TABLES LIKE '$click_table'") == $click_table) {
$clicks = $wpdb->get_var($wpdb->prepare("
SELECT COUNT(id) FROM $click_table
WHERE date >= %s AND date <= %s",
$start_date, $end_date
));
}
return (object) [
'referrals' => $referrals,
'sales' => round($sales, 2),
'discount' => round($discount, 2),
'commission' => round($commission, 2),
'clicks' => $clicks
];
}
Notice: Function register_block_patterns was called incorrectly. Could not register pattern "/var/www/vhosts/neuronsit.in/marketing.neuronsit.in/wp-content/plugins/woocommerce/patterns/current.php" as a block pattern ("Slug" field missing) Please see Debugging in WordPress for more information. (This message was added in version 6.0.0.) in /var/www/vhosts/neuronsit.in/marketing.neuronsit.in/wp-includes/functions.php on line 6114