$user_id, 'limit' => -1, 'orderby' => 'date', 'order' => 'DESC', )); if (empty($orders)) { wp_send_json_error(__('No orders found.', 'mxchat')); wp_die(); } $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()); $order_data[] = array( 'order_id' => $order_id, 'order_date' => $order_date, 'order_total' => $order_total, 'order_status' => $order_status, 'order_items' => $order_items, ); } // Send the order data as a success response wp_send_json_success($order_data); 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(); } // Check if WooCommerce order access is enabled if (!self::is_order_access_enabled()) { return "Order tracking is disabled. Please contact support for more information."; } // Check if the user is logged in 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; } // Function to check if order access is enabled in the admin settings private static function is_order_access_enabled() { $options = get_option('mxchat_options', array()); return isset($options['enable_woocommerce_order_access']) && $options['enable_woocommerce_order_access'] === '1'; } } MxChat_WooCommerce::init();