$user_id, 'limit' => -1, 'orderby' => 'date', 'order' => 'DESC', )); // Log the raw orders for debugging if (empty($orders)) { error_log('No orders found for user ID: ' . $user_id); wp_send_json_error(__('No orders found.', 'mxchat')); wp_die(); } // Detailed logging of each order $order_data = array(); foreach ($orders as $order) { $order_id = $order->get_id(); $order_date = $order->get_date_created()->date('F j, Y'); $order_total = wc_price($order->get_total()); $order_status = wc_get_order_status_name($order->get_status()); $order_items = array_map(function ($item) { return $item->get_name(); }, $order->get_items()); error_log("Order ID: {$order_id}, Date: {$order_date}, Total: {$order_total}, Status: {$order_status}, Items: " . implode(', ', $order_items)); $order_data[] = array( 'order_id' => $order_id, 'order_date' => $order_date, 'order_total' => $order_total, 'order_status' => $order_status, 'order_items' => $order_items, ); } // If the order data is populated, send it as a success response if (!empty($order_data)) { wp_send_json_success($order_data); } else { error_log('No order data was processed.'); wp_send_json_error(__('No orders found.', 'mxchat')); } wp_die(); } // Helper function to determine if the query is related to orders public static function mxchat_is_order_related_query($message) { $keywords = array('my orders', 'order status', 'previous orders', 'order history'); foreach ($keywords as $keyword) { if (stripos($message, $keyword) !== false) { return true; } } return false; } // Function to fetch the user's order details as a string public static function mxchat_fetch_user_orders_details() { // Ensure the session is active if (!session_id()) { session_start(); } // Double-check user authentication if (!is_user_logged_in()) { return 'User is not logged in.'; } // Fetch the logged-in user ID $user_id = get_current_user_id(); // Fetch all orders associated with the user, regardless of status $orders = wc_get_orders(array( 'customer_id' => $user_id, 'limit' => -1, 'orderby' => 'date', 'order' => 'DESC', )); if (empty($orders)) { return 'No orders found for the user.'; } $order_details = "Your recent orders:\n"; foreach ($orders as $order) { $items = implode(', ', array_map(function ($item) { return $item->get_name(); }, $order->get_items())); $order_details .= "Order #{$order->get_id()} on {$order->get_date_created()->date('F j, Y')} - Status: {$order->get_status()} - Total: {$order->get_total()} - Items: {$items}\n"; } return $order_details; } } MxChat_WooCommerce::init();