vector_store = new Store(); add_action('woocommerce_update_product', [$this, 'queue_product_index'], 10, 2); add_action('woocommerce_new_product', [$this, 'queue_product_index'], 10, 2); add_action('before_delete_post', [$this, 'handle_deleted_post']); add_action('woocommerce_trash_product', [$this, 'handle_deleted_post']); add_action('untrash_post', [$this, 'handle_untrashed_post']); add_action('shoponix_process_single_product_index', [$this, 'process_single_product']); } public function handle_untrashed_post($post_id) { if (get_post_type($post_id) === 'product') { $this->queue_product_index($post_id); } } public function queue_product_index($product_id, $product = null) { if (function_exists('as_enqueue_async_action')) { as_enqueue_async_action('shoponix_process_single_product_index', ['product_id' => $product_id], 'shoponix'); } else { $this->process_single_product($product_id); // Fallback if Action Scheduler is missing } } public function handle_deleted_post($post_id) { if (get_post_type($post_id) === 'product') { $this->vector_store->delete_vector($post_id); } } public function index_all_products() { $products = wc_get_products(['limit' => -1, 'status' => 'publish', 'return' => 'ids']); $this->vector_store->clear_vectors(); $count = 0; foreach ($products as $product_id) { $this->queue_product_index($product_id); $count++; } return $count; } public function process_single_product($product_id) { $product = wc_get_product($product_id); if (!$product || $product->get_status() !== 'publish') { $this->vector_store->delete_vector($product_id); return; } $api_key_encrypted = get_option('shoponix_mistral_api_key'); if (!$api_key_encrypted) return; $api_key = \Shoponix\Security\Encryption::decrypt($api_key_encrypted); $provider = get_option('shoponix_ai_provider', 'mistral'); $embedding_model = get_option('shoponix_embedding_model'); $embedding_client = new \Shoponix\AI\UniversalEmbedder($provider, $api_key, $embedding_model); $content = $this->build_product_text($product); $embedding_vector = $embedding_client->embed($content); if ($embedding_vector) { // Delete old vector before inserting new one to avoid duplicates $this->vector_store->delete_vector($product_id); $this->vector_store->add_vector($product_id, $content, $embedding_vector); } } private function build_product_text($product) { $name = $product->get_name(); $short_desc = $product->get_short_description(); $long_desc = $product->get_description(); $categories = implode(', ', wp_list_pluck(wc_get_product_terms($product->get_id(), 'product_cat'), 'name')); $tags = implode(', ', wp_list_pluck(wc_get_product_terms($product->get_id(), 'product_tag'), 'name')); $type = $product->get_type(); $price = $product->get_price(); $regular_price = $product->get_regular_price(); $sale_price = $product->get_sale_price(); $sku = $product->get_sku(); $stock_status = $product->get_stock_status() === 'instock' ? 'In Stock' : 'Out of Stock'; $permalink = $product->get_permalink(); $rating = $product->get_average_rating(); // Add attributes (e.g., Color, Size) $attributes = []; foreach ($product->get_attributes() as $attr_name => $attr) { if ($attr->is_taxonomy()) { $terms = wp_get_post_terms($product->get_id(), $attr_name, ['fields' => 'names']); $attributes[] = $attr_name . ': ' . implode(', ', $terms); } else { $attributes[] = $attr_name . ': ' . implode(', ', $attr->get_options()); } } $attr_text = implode('; ', $attributes); $parts = [ "Name: $name", "Type: $type", "URL: $permalink", "Rating: $rating", $short_desc, $long_desc, "Categories: $categories", "Tags: $tags", "Attributes: $attr_text", "Price: $price", "Regular Price: $regular_price", "Sale Price: $sale_price", "SKU: $sku", "Status: $stock_status" ]; // Retrieve and index variations for variable products if ($product->is_type('variable')) { $variation_texts = []; $variations = $product->get_available_variations() ?: $product->get_children(); foreach ($variations as $var_item) { // If children are IDs, fetch product object $var_id = is_array($var_item) ? ($var_item['variation_id'] ?? 0) : $var_item; $variation = wc_get_product($var_id); if ($variation) { $var_attributes = []; foreach ($variation->get_attributes() as $v_attr_name => $v_attr_val) { $var_attributes[] = "$v_attr_name: $v_attr_val"; } $variation_texts[] = "Variation ID: $var_id (Attributes: " . implode(', ', $var_attributes) . " | Price: " . $variation->get_price() . " | Status: " . ($variation->is_in_stock() ? 'In Stock' : 'Out of Stock') . ")"; } } if (!empty($variation_texts)) { $parts[] = "Available Variations: " . implode('; ', $variation_texts); } } $parts = array_filter($parts); return implode('. ', $parts); } }