PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 1.1.5
MxChat – AI Chatbot & Content Generation for WordPress v1.1.5
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.1.5, at includes/class-mxchat-woocommerce.php

206 lines 7.2 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 // Check if WooCommerce order access is enabled in the settings
17 if (!self::is_order_access_enabled()) {
18 wp_send_json_error(__('Order tracking is disabled. Please contact support for more information.', 'mxchat'));
19 wp_die();
20 }
21
22 // Validate the AJAX request with a nonce
23 if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_nonce')) {
24 wp_send_json_error(__('Invalid request.', 'mxchat'));
25 wp_die();
26 }
27
28 // Check if the user is logged in
29 if (!is_user_logged_in()) {
30 wp_send_json_error(__('You need to be logged in to view your orders.', 'mxchat'));
31 wp_die();
32 }
33
34 // Fetch the logged-in user ID
35 $user_id = get_current_user_id();
36
37 // Fetch orders associated with the user
38 $orders = wc_get_orders(array(
39 'customer_id' => $user_id,
40 'limit' => -1,
41 'orderby' => 'date',
42 'order' => 'DESC',
43 ));
44
45 if (empty($orders)) {
46 wp_send_json_error(__('No orders found.', 'mxchat'));
47 wp_die();
48 }
49
50 $order_data = array();
51 foreach ($orders as $order) {
52 $order_id = $order->get_id();
53 $order_date = $order->get_date_created()->date('F j, Y');
54 $order_total = wc_price($order->get_total());
55 $order_status = wc_get_order_status_name($order->get_status());
56 $order_items = array_map(function ($item) {
57 return $item->get_name();
58 }, $order->get_items());
59
60 $order_data[] = array(
61 'order_id' => $order_id,
62 'order_date' => $order_date,
63 'order_total' => $order_total,
64 'order_status' => $order_status,
65 'order_items' => $order_items,
66 );
67 }
68
69 // Send the order data as a success response
70 wp_send_json_success($order_data);
71 wp_die();
72 }
73
74 // Helper function to determine if the query is related to orders
75 public static function mxchat_is_order_related_query($message) {
76 $keywords = array('my orders', 'order status', 'previous orders', 'order history');
77 foreach ($keywords as $keyword) {
78 if (stripos($message, $keyword) !== false) {
79 return true;
80 }
81 }
82 return false;
83 }
84
85 // Function to fetch the user's order details as a string
86 public static function mxchat_fetch_user_orders_details() {
87 // Ensure the session is active
88 if (!session_id()) {
89 session_start();
90 }
91
92 // Check if WooCommerce order access is enabled
93 if (!self::is_order_access_enabled()) {
94 return "Order tracking is disabled. Please contact support for more information.";
95 }
96
97 // Check if the user is logged in
98 if (!is_user_logged_in()) {
99 return 'User is not logged in.';
100 }
101
102 // Fetch the logged-in user ID
103 $user_id = get_current_user_id();
104
105 // Fetch all orders associated with the user, regardless of status
106 $orders = wc_get_orders(array(
107 'customer_id' => $user_id,
108 'limit' => -1,
109 'orderby' => 'date',
110 'order' => 'DESC',
111 ));
112
113 if (empty($orders)) {
114 return 'No orders found for the user.';
115 }
116
117 $order_details = "Your recent orders:\n";
118 foreach ($orders as $order) {
119 $items = implode(', ', array_map(function ($item) {
120 return $item->get_name();
121 }, $order->get_items()));
122
123 $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";
124 }
125
126 return $order_details;
127 }
128
129 // Function to check if order access is enabled in the admin settings
130 public static function is_order_access_enabled() {
131 $options = get_option('mxchat_options', array());
132 return isset($options['enable_woocommerce_order_access']) && $options['enable_woocommerce_order_access'] === '1';
133 }
134
135 // Check if there are items in the cart
136 public static function cart_has_items() {
137 return WC()->cart && WC()->cart->get_cart_contents_count() > 0;
138 }
139
140 public static function mxchat_extract_product_id_from_message($message) {
141 // Ensure WooCommerce functions are available
142 if (!function_exists('wc_get_products')) {
143 error_log("WooCommerce is not active or not available.");
144 return null; // Exit if WooCommerce is not active
145 }
146
147 // Normalize the message
148 $message = sanitize_text_field(strtolower($message));
149
150 // Add debug log for the incoming message
151 error_log("Product extraction message: " . $message);
152
153 // Split the message into individual words to search more effectively
154 $search_terms = explode(' ', $message);
155
156 // Log the search terms
157 error_log("Search terms: " . implode(', ', $search_terms));
158
159 // Search for the product in WooCommerce catalog by title, slug, and SKU
160 $args = array(
161 'status' => 'publish',
162 'limit' => 5, // Retrieve multiple products to better match
163 'return' => 'objects',
164 );
165
166 $products = wc_get_products($args);
167
168 // Log how many products were fetched
169 error_log("Total products found: " . count($products));
170
171 foreach ($products as $product) {
172 // Check if the product title, slug, or SKU matches any of the search terms
173 $product_name = strtolower($product->get_name());
174 $product_slug = strtolower($product->get_slug());
175 $product_sku = strtolower($product->get_sku());
176
177 foreach ($search_terms as $term) {
178 if (stripos($product_name, $term) !== false || stripos($product_slug, $term) !== false || stripos($product_sku, $term) !== false) {
179 // Log which product was matched
180 error_log("Matched product: " . $product_name . " (ID: " . $product->get_id() . ")");
181 return $product->get_id(); // Return the first matching product ID
182 }
183 }
184 }
185
186 // If no product matches, log the failure
187 error_log("No product matched for message: " . $message);
188
189 return null;
190 }
191
192
193 // Store the product ID in session when discussed
194 public static function store_last_discussed_product($product_id) {
195 set_transient('mxchat_last_product', $product_id, 12 * HOUR_IN_SECONDS);
196 }
197
198 // Retrieve the last discussed product ID
199 public static function get_last_discussed_product() {
200 return get_transient('mxchat_last_product');
201 }
202
203 }
204
205 MxChat_WooCommerce::init();
206