| 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 |
add_action('wp_ajax_mxchat_add_to_cart', array(__CLASS__, 'mxchat_add_to_cart')); |
| 14 |
add_action('wp_ajax_nopriv_mxchat_add_to_cart', array(__CLASS__, 'mxchat_add_to_cart')); |
| 15 |
|
| 16 |
} |
| 17 |
|
| 18 |
// New function to handle add to cart requests |
| 19 |
public static function mxchat_add_to_cart() { |
| 20 |
// Validate nonce |
| 21 |
if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_nonce')) { |
| 22 |
wp_send_json_error('Invalid nonce.'); |
| 23 |
wp_die(); |
| 24 |
} |
| 25 |
|
| 26 |
$product_id = isset($_POST['product_id']) ? intval($_POST['product_id']) : 0; |
| 27 |
if (!$product_id) { |
| 28 |
wp_send_json_error('Invalid product ID.'); |
| 29 |
wp_die(); |
| 30 |
} |
| 31 |
|
| 32 |
// Add to WooCommerce cart |
| 33 |
$added = WC()->cart->add_to_cart($product_id); |
| 34 |
if ($added) { |
| 35 |
$product = wc_get_product($product_id); |
| 36 |
wp_send_json_success(['message' => "The product '{$product->get_name()}' has been added to your cart."]); |
| 37 |
} else { |
| 38 |
wp_send_json_error('Error adding product to cart.'); |
| 39 |
} |
| 40 |
wp_die(); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
// Function to fetch the user's order details with optional filters |
| 45 |
public static function mxchat_fetch_user_orders_details($type = 'all') { |
| 46 |
// Ensure the session is active |
| 47 |
if (!session_id()) { |
| 48 |
session_start(); |
| 49 |
} |
| 50 |
|
| 51 |
// Check if the user is logged in |
| 52 |
if (!is_user_logged_in()) { |
| 53 |
return 'User is not logged in.'; |
| 54 |
} |
| 55 |
|
| 56 |
// Fetch the logged-in user ID |
| 57 |
$user_id = get_current_user_id(); |
| 58 |
|
| 59 |
// Define query arguments based on type |
| 60 |
$args = array( |
| 61 |
'customer_id' => $user_id, |
| 62 |
'limit' => ($type === 'last') ? 1 : -1, // Fetch the last order if specified |
| 63 |
'orderby' => 'date', |
| 64 |
'order' => 'DESC', |
| 65 |
); |
| 66 |
|
| 67 |
// Retrieve orders based on the query |
| 68 |
$orders = wc_get_orders($args); |
| 69 |
if (empty($orders)) { |
| 70 |
return 'No orders found for the user.'; |
| 71 |
} |
| 72 |
|
| 73 |
// Prepare the order details message |
| 74 |
$order_details = ($type === 'last') ? "Your last order:\n" : "Your recent orders:\n"; |
| 75 |
foreach ($orders as $order) { |
| 76 |
$items = implode(', ', array_map(function ($item) { |
| 77 |
return $item->get_name(); |
| 78 |
}, $order->get_items())); |
| 79 |
|
| 80 |
$order_details .= "Order #{$order->get_id()} on {$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}\n"; |
| 81 |
|
| 82 |
// Show only the last order if specified |
| 83 |
if ($type === 'last') { |
| 84 |
break; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return $order_details; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
// Check if there are items in the cart |
| 93 |
public static function cart_has_items() { |
| 94 |
return WC()->cart && WC()->cart->get_cart_contents_count() > 0; |
| 95 |
} |
| 96 |
|
| 97 |
public static function mxchat_extract_product_id_from_message($message) { |
| 98 |
// Ensure WooCommerce functions are available |
| 99 |
if (!function_exists('wc_get_products')) { |
| 100 |
error_log("WooCommerce is not active or not available."); |
| 101 |
return null; // Exit if WooCommerce is not active |
| 102 |
} |
| 103 |
|
| 104 |
// Normalize the message |
| 105 |
$message = sanitize_text_field(strtolower($message)); |
| 106 |
|
| 107 |
// Add debug log for the incoming message |
| 108 |
error_log("Product extraction message: " . $message); |
| 109 |
|
| 110 |
// Split the message into individual words to search more effectively |
| 111 |
$search_terms = explode(' ', $message); |
| 112 |
|
| 113 |
// Log the search terms |
| 114 |
error_log("Search terms: " . implode(', ', $search_terms)); |
| 115 |
|
| 116 |
// Search for the product in WooCommerce catalog by title, slug, and SKU |
| 117 |
$args = array( |
| 118 |
'status' => 'publish', |
| 119 |
'limit' => 5, // Retrieve multiple products to better match |
| 120 |
'return' => 'objects', |
| 121 |
); |
| 122 |
|
| 123 |
$products = wc_get_products($args); |
| 124 |
|
| 125 |
// Log how many products were fetched |
| 126 |
error_log("Total products found: " . count($products)); |
| 127 |
|
| 128 |
foreach ($products as $product) { |
| 129 |
// Check if the product title, slug, or SKU matches any of the search terms |
| 130 |
$product_name = strtolower($product->get_name()); |
| 131 |
$product_slug = strtolower($product->get_slug()); |
| 132 |
$product_sku = strtolower($product->get_sku()); |
| 133 |
|
| 134 |
foreach ($search_terms as $term) { |
| 135 |
if (stripos($product_name, $term) !== false || stripos($product_slug, $term) !== false || stripos($product_sku, $term) !== false) { |
| 136 |
// Log which product was matched |
| 137 |
error_log("Matched product: " . $product_name . " (ID: " . $product->get_id() . ")"); |
| 138 |
return $product->get_id(); // Return the first matching product ID |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
// If no product matches, log the failure |
| 144 |
error_log("No product matched for message: " . $message); |
| 145 |
|
| 146 |
return null; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
// Store the product ID in session when discussed |
| 151 |
public static function store_last_discussed_product($product_id) { |
| 152 |
set_transient('mxchat_last_product', $product_id, 12 * HOUR_IN_SECONDS); |
| 153 |
} |
| 154 |
|
| 155 |
// Retrieve the last discussed product ID |
| 156 |
public static function get_last_discussed_product() { |
| 157 |
return get_transient('mxchat_last_product'); |
| 158 |
} |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
MxChat_WooCommerce::init(); |
| 163 |
|