namespace, '/dashboard', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_dashboard' ), 'permission_callback' => array( $this, 'check_permission' ), ), ) ); register_rest_route( $this->namespace, '/dashboard/stats', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_stats' ), 'permission_callback' => array( $this, 'check_permission' ), ), ) ); } /** * Get dashboard data. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function get_dashboard( $request ) { $customer_id = $this->get_customer_id(); $customer = new WC_Customer( $customer_id ); $data = array( 'user' => $this->get_user_data( $customer ), 'stats' => $this->get_customer_stats( $customer_id ), 'recent_orders' => $this->get_recent_orders( $customer_id ), 'quick_actions' => $this->get_quick_actions(), 'completeness' => $this->get_profile_completeness( $customer ), ); return rest_ensure_response( $data ); } /** * Get stats data. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function get_stats( $request ) { $customer_id = $this->get_customer_id(); $stats = $this->get_customer_stats( $customer_id ); return rest_ensure_response( $stats ); } /** * Get user data. * * @param WC_Customer $customer Customer object. * @return array */ private function get_user_data( $customer ) { $customer_id = $customer->get_id(); $user = get_userdata( $customer_id ); // Get first/last name with fallback to user meta. $first_name = $customer->get_first_name(); if ( empty( $first_name ) && $user ) { $first_name = get_user_meta( $customer_id, 'first_name', true ); } $last_name = $customer->get_last_name(); if ( empty( $last_name ) && $user ) { $last_name = get_user_meta( $customer_id, 'last_name', true ); } $display_name = $user ? $user->display_name : ''; $email = $customer->get_email(); if ( empty( $email ) && $user ) { $email = $user->user_email; } return array( 'id' => $customer_id, 'email' => $email, 'first_name' => $first_name, 'last_name' => $last_name, 'display_name' => $display_name, 'avatar_url' => get_avatar_url( $customer_id, array( 'size' => 96 ) ), 'member_since' => $user ? $this->format_date( $user->user_registered ) : '', ); } /** * Get customer statistics. * * @param int $customer_id Customer ID. * @return array */ private function get_customer_stats( $customer_id ) { $customer = new WC_Customer( $customer_id ); // Get order count. $order_count = $customer->get_order_count(); // Get total spent. $total_spent = $customer->get_total_spent(); // Get orders this month. $orders_this_month = $this->get_orders_count_period( $customer_id, 'month' ); // Get pending orders. $pending_orders = $this->get_pending_orders_count( $customer_id ); return array( 'total_orders' => $order_count, 'total_spent' => $this->format_price( $total_spent ), 'total_spent_raw' => (float) $total_spent, 'orders_this_month' => $orders_this_month, 'pending_orders' => $pending_orders, 'currency' => get_woocommerce_currency(), 'currency_symbol' => html_entity_decode( get_woocommerce_currency_symbol() ), ); } /** * Get orders count for a period. * * @param int $customer_id Customer ID. * @param string $period Period (month, year). * @return int */ private function get_orders_count_period( $customer_id, $period = 'month' ) { $date_after = ''; switch ( $period ) { case 'month': $date_after = gmdate( 'Y-m-01' ); break; case 'year': $date_after = gmdate( 'Y-01-01' ); break; } $orders = wc_get_orders( array( 'customer_id' => $customer_id, 'date_after' => $date_after, 'return' => 'ids', 'limit' => -1, ) ); return count( $orders ); } /** * Get pending orders count. * * @param int $customer_id Customer ID. * @return int */ private function get_pending_orders_count( $customer_id ) { $orders = wc_get_orders( array( 'customer_id' => $customer_id, 'status' => array( 'pending', 'processing', 'on-hold' ), 'return' => 'ids', 'limit' => -1, ) ); return count( $orders ); } /** * Get recent orders. * * @param int $customer_id Customer ID. * @param int $limit Number of orders to return. * @return array */ private function get_recent_orders( $customer_id, $limit = 5 ) { $orders = wc_get_orders( array( 'customer_id' => $customer_id, 'limit' => $limit, 'orderby' => 'date', 'order' => 'DESC', ) ); $data = array(); foreach ( $orders as $order ) { // Get date safely. $date_created = $order->get_date_created(); $date_string = $date_created ? $date_created->date( 'Y-m-d H:i:s' ) : ''; $data[] = array( 'id' => $order->get_id(), 'number' => $order->get_order_number(), 'status' => $order->get_status(), 'status_label' => wc_get_order_status_name( $order->get_status() ), 'date' => $date_string ? $this->format_date( $date_string ) : __( 'N/A', 'advanced-customer-account' ), 'total' => $this->format_price( $order->get_total() ), 'items_count' => $order->get_item_count(), ); } return $data; } /** * Get quick actions. * * @return array */ private function get_quick_actions() { return array( array( 'id' => 'edit_profile', 'label' => __( 'Edit Profile', 'advanced-customer-account' ), 'icon' => 'admin-users', 'link' => '#profile', ), array( 'id' => 'view_orders', 'label' => __( 'View Orders', 'advanced-customer-account' ), 'icon' => 'cart', 'link' => '#orders', ), array( 'id' => 'edit_addresses', 'label' => __( 'Edit Addresses', 'advanced-customer-account' ), 'icon' => 'location', 'link' => '#addresses', ), array( 'id' => 'browse_shop', 'label' => __( 'Browse Shop', 'advanced-customer-account' ), 'icon' => 'store', 'link' => wc_get_page_permalink( 'shop' ), ), ); } /** * Get profile completeness percentage. * * @param WC_Customer $customer Customer object. * @return array */ private function get_profile_completeness( $customer ) { $fields = array( 'first_name' => ! empty( $customer->get_first_name() ), 'last_name' => ! empty( $customer->get_last_name() ), 'email' => ! empty( $customer->get_email() ), 'phone' => ! empty( $customer->get_billing_phone() ), 'billing_address' => ! empty( $customer->get_billing_address_1() ), 'shipping_address' => ! empty( $customer->get_shipping_address_1() ), ); $completed = array_filter( $fields ); $total = count( $fields ); $done = count( $completed ); return array( 'percentage' => round( ( $done / $total ) * 100 ), 'completed' => $done, 'total' => $total, 'missing' => array_keys( array_diff_key( $fields, $completed ) ), ); } }