From a2dd6a98a3a62d2b997ce3832c9d6f1be023bba6 Mon Sep 17 00:00:00 2001 From: dwindown Date: Tue, 4 Nov 2025 18:36:45 +0700 Subject: [PATCH] =?UTF-8?q?feat:=20Implement=20conversion=20rate=20as=20(C?= =?UTF-8?q?ompleted=20Orders=20/=20Total=20Orders)=20=C3=97=20100=20and=20?= =?UTF-8?q?fix=20Recharts=20prop=20warning?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin-spa/src/routes/Dashboard/index.tsx | 4 ++-- includes/Api/AnalyticsController.php | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) 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, ], ],