PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 1.0.8
MxChat – AI Chatbot & Content Generation for WordPress v1.0.8
3.2.21 3.2.20 3.2.19 3.2.18 3.2.17 3.2.16 3.2.15 3.2.14 3.2.12 3.2.13 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 3.2.6 3.2.5 3.2.4 3.2.3 3.2.2 3.2.1 2.0.3 2.0.4 2.0.5 2.0.6 All 152 releases
mxchat-basic / includes / class-mxchat-woocommerce.php

class-mxchat-woocommerce.php in MxChat – AI Chatbot & Content Generation for WordPress 1.0.8, at includes/class-mxchat-woocommerce.php

133 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) {
4 exit;
5 }
6
7 class MxChat_WooCommerce {
8
9 public static function init() {
10 add_action('wp_ajax_mxchat_fetch_user_orders', array(__CLASS__, 'mxchat_fetch_user_orders'));
11 add_action('wp_ajax_nopriv_mxchat_fetch_user_orders', array(__CLASS__, 'mxchat_fetch_user_orders'));
12 }
13
14 // Fetch user orders via AJAX
15 public static function mxchat_fetch_user_orders() {
16 // Validate the AJAX request with a nonce
17 if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_nonce')) {
18 wp_send_json_error(__('Invalid request.', 'mxchat'));
19 wp_die();
20 }
21
22 // Check if the user is logged in
23 if (!is_user_logged_in()) {
24 error_log('User is not logged in.');
25 wp_send_json_error(__('You need to be logged in to view your orders.', 'mxchat'));
26 wp_die();
27 }
28
29 // Fetch the logged-in user ID
30 $user_id = get_current_user_id();
31 error_log('User ID: ' . $user_id);
32
33 // Fetch orders associated with the user
34 $orders = wc_get_orders(array(
35 'customer_id' => $user_id,
36 'limit' => -1,
37 'orderby' => 'date',
38 'order' => 'DESC',
39 ));
40
41 // Log the raw orders for debugging
42 if (empty($orders)) {
43 error_log('No orders found for user ID: ' . $user_id);
44 wp_send_json_error(__('No orders found.', 'mxchat'));
45 wp_die();
46 }
47
48 // Detailed logging of each order
49 $order_data = array();
50 foreach ($orders as $order) {
51 $order_id = $order->get_id();
52 $order_date = $order->get_date_created()->date('F j, Y');
53 $order_total = wc_price($order->get_total());
54 $order_status = wc_get_order_status_name($order->get_status());
55 $order_items = array_map(function ($item) {
56 return $item->get_name();
57 }, $order->get_items());
58
59 error_log("Order ID: {$order_id}, Date: {$order_date}, Total: {$order_total}, Status: {$order_status}, Items: " . implode(', ', $order_items));
60
61 $order_data[] = array(
62 'order_id' => $order_id,
63 'order_date' => $order_date,
64 'order_total' => $order_total,
65 'order_status' => $order_status,
66 'order_items' => $order_items,
67 );
68 }
69
70 // If the order data is populated, send it as a success response
71 if (!empty($order_data)) {
72 wp_send_json_success($order_data);
73 } else {
74 error_log('No order data was processed.');
75 wp_send_json_error(__('No orders found.', 'mxchat'));
76 }
77
78 wp_die();
79 }
80
81 // Helper function to determine if the query is related to orders
82 public static function mxchat_is_order_related_query($message) {
83 $keywords = array('my orders', 'order status', 'previous orders', 'order history');
84 foreach ($keywords as $keyword) {
85 if (stripos($message, $keyword) !== false) {
86 return true;
87 }
88 }
89 return false;
90 }
91
92 // Function to fetch the user's order details as a string
93 public static function mxchat_fetch_user_orders_details() {
94 // Ensure the session is active
95 if (!session_id()) {
96 session_start();
97 }
98
99 // Double-check user authentication
100 if (!is_user_logged_in()) {
101 return 'User is not logged in.';
102 }
103
104 // Fetch the logged-in user ID
105 $user_id = get_current_user_id();
106
107 // Fetch all orders associated with the user, regardless of status
108 $orders = wc_get_orders(array(
109 'customer_id' => $user_id,
110 'limit' => -1,
111 'orderby' => 'date',
112 'order' => 'DESC',
113 ));
114
115 if (empty($orders)) {
116 return 'No orders found for the user.';
117 }
118
119 $order_details = "Your recent orders:\n";
120 foreach ($orders as $order) {
121 $items = implode(', ', array_map(function ($item) {
122 return $item->get_name();
123 }, $order->get_items()));
124
125 $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";
126 }
127
128 return $order_details;
129 }
130 }
131
132 MxChat_WooCommerce::init();
133