diff --git a/admin-spa/src/routes/Dashboard/index.tsx b/admin-spa/src/routes/Dashboard/index.tsx index cc29a09..afdc386 100644 --- a/admin-spa/src/routes/Dashboard/index.tsx +++ b/admin-spa/src/routes/Dashboard/index.tsx @@ -511,8 +511,8 @@ export default function Dashboard() { > entry.count || entry.value || 0} - nameKey={(entry) => entry.status || entry.name || 'Unknown'} + dataKey="count" + nameKey="status" cx="50%" cy="50%" innerRadius={70} diff --git a/includes/Api/AnalyticsController.php b/includes/Api/AnalyticsController.php index 7a6f132..de73d95 100644 --- a/includes/Api/AnalyticsController.php +++ b/includes/Api/AnalyticsController.php @@ -368,7 +368,7 @@ class AnalyticsController { ORDER BY count DESC "); - // Format status distribution + // Format status distribution and calculate conversion rate $formatted_status = []; $status_colors = [ 'wc-completed' => '#10b981', @@ -380,11 +380,21 @@ class AnalyticsController { 'wc-failed' => '#dc2626', ]; + $total_all_orders = 0; + $completed_orders = 0; + foreach ($orderStatusDistribution as $status) { $status_name = str_replace('wc-', '', $status->status); + $count = intval($status->count); + + $total_all_orders += $count; + if ($status->status === 'wc-completed') { + $completed_orders = $count; + } + $formatted_status[] = [ 'status' => ucfirst($status_name), - 'count' => intval($status->count), + 'count' => $count, 'color' => $status_colors[$status->status] ?? '#6b7280', ]; } @@ -392,6 +402,9 @@ class AnalyticsController { // Calculate metrics $avg_order_value = $total_orders > 0 ? round($total_revenue / $total_orders, 2) : 0; + // Calculate conversion rate: (Completed Orders / Total Orders) × 100 + $conversion_rate = $total_all_orders > 0 ? round(($completed_orders / $total_all_orders) * 100, 2) : 0.00; + // Get top products $products_table = $wpdb->prefix . 'wc_order_product_lookup'; $top_products = $wpdb->get_results($wpdb->prepare(" @@ -465,8 +478,8 @@ class AnalyticsController { 'change' => 0, ], 'conversionRate' => [ - 'today' => 0, // TODO: Calculate - 'yesterday' => 0, + 'today' => $conversion_rate, + 'yesterday' => 0, // TODO: Calculate previous period 'change' => 0, ], ],