cart->add_to_cart($product_id); if ($added) { $product = wc_get_product($product_id); wp_send_json_success(['message' => "The product '{$product->get_name()}' has been added to your cart."]); } else { wp_send_json_error('Error adding product to cart.'); } wp_die(); } // Function to fetch the user's order details with optional filters public static function mxchat_fetch_user_orders_details($type = 'all') { // Check if the user is logged in if (!is_user_logged_in()) { return []; // Return an empty array if the user is not logged in } // Fetch the logged-in user ID $user_id = get_current_user_id(); // Define query arguments based on the requested type $args = array( 'customer_id' => $user_id, 'limit' => ($type === 'last') ? 1 : -1, // Fetch only the last order if specified 'orderby' => 'date', 'order' => 'DESC', ); // Retrieve orders based on the query $orders = wc_get_orders($args); if (empty($orders)) { return []; // Return an empty array if no orders are found } // Prepare an array of order details $order_details = []; foreach ($orders as $order) { $items = array_map(function ($item) { return $item->get_name(); }, $order->get_items()); $order_details[] = array( 'order_id' => $order->get_id(), 'date' => $order->get_date_created()->date('F j, Y'), 'status' => wc_get_order_status_name($order->get_status()), 'total' => wc_price($order->get_total()), 'items' => $items, ); // If only the last order is needed, break after the first iteration if ($type === 'last') { break; } } return $order_details; // Return the structured order details } // Check if there are items in the cart public static function cart_has_items() { return WC()->cart && WC()->cart->get_cart_contents_count() > 0; } public static function mxchat_extract_product_id_from_message($message) { // Ensure WooCommerce functions are available if (!function_exists('wc_get_products')) { error_log("WooCommerce is not active or not available."); return null; // Exit if WooCommerce is not active } // Normalize the message $message = sanitize_text_field(strtolower($message)); // Add debug log for the incoming message error_log("Product extraction message: " . $message); // Split the message into individual words to search more effectively $search_terms = explode(' ', $message); // Log the search terms error_log("Search terms: " . implode(', ', $search_terms)); // Search for the product in WooCommerce catalog by title, slug, and SKU $args = array( 'status' => 'publish', 'limit' => 5, // Retrieve multiple products to better match 'return' => 'objects', ); $products = wc_get_products($args); // Log how many products were fetched error_log("Total products found: " . count($products)); foreach ($products as $product) { // Check if the product title, slug, or SKU matches any of the search terms $product_name = strtolower($product->get_name()); $product_slug = strtolower($product->get_slug()); $product_sku = strtolower($product->get_sku()); foreach ($search_terms as $term) { if (stripos($product_name, $term) !== false || stripos($product_slug, $term) !== false || stripos($product_sku, $term) !== false) { // Log which product was matched error_log("Matched product: " . $product_name . " (ID: " . $product->get_id() . ")"); return $product->get_id(); // Return the first matching product ID } } } // If no product matches, log the failure error_log("No product matched for message: " . $message); return null; } // Store the product ID in session when discussed public static function store_last_discussed_product($product_id) { set_transient('mxchat_last_product', $product_id, 12 * HOUR_IN_SECONDS); } // Retrieve the last discussed product ID public static function get_last_discussed_product() { return get_transient('mxchat_last_product'); } } MxChat_WooCommerce::init();