container = $container; } /** * Extracts and maps data from a WooCommerce product into our custom index format. * * @param int $product_id * @return array|false Returns mapped data array or false if not a valid product. */ public function map_product_data($product_id) { if (!function_exists('wc_get_product')) { return false; } $product = wc_get_product($product_id); if (!$product) { return false; } // We only index published products by default if ($product->get_status() !== 'publish') { return false; } // Get basic data $title = $product->get_name(); $content = $product->get_description(); $excerpt = $product->get_short_description(); $sku = $product->get_sku(); // Collect taxonomies (Categories, Tags, Brands) $taxonomies_terms = []; $taxonomies = get_object_taxonomies('product'); foreach ($taxonomies as $taxonomy) { $terms = wp_get_post_terms($product_id, $taxonomy, ['fields' => 'names']); if (!is_wp_error($terms) && !empty($terms)) { $taxonomies_terms = array_merge($taxonomies_terms, $terms); } } // Collect Attributes $attributes_terms = []; $attributes = $product->get_attributes(); foreach ($attributes as $attribute) { if ($attribute->is_taxonomy()) { $terms = wp_get_post_terms($product_id, $attribute->get_name(), ['fields' => 'names']); if (!is_wp_error($terms) && !empty($terms)) { $attributes_terms = array_merge($attributes_terms, $terms); } } else { $options = $attribute->get_options(); if (!empty($options)) { $attributes_terms = array_merge($attributes_terms, $options); } } } // Prepare the massive searchable string // We combine content, excerpt, taxonomies, attributes for fulltext searching. // Title and SKU get their own dedicated columns for higher weighting, but we also include them in search_terms. $raw_search_terms = implode(' ', array_merge( [$title, $sku, strip_tags($content), strip_tags($excerpt)], $taxonomies_terms, $attributes_terms )); // Clean up the string (lowercase, remove punctuation, stop words, etc.) $search_terms = $this->sanitize_and_tokenize($raw_search_terms); // Fetch parent ID if it's a variation $parent_id = $product->get_parent_id(); $variation_id = ($product->is_type('variation')) ? $product_id : 0; $object_id = ($parent_id > 0) ? $parent_id : $product_id; // Current Language (if WPML is active) $lang = apply_filters('wpml_post_language_details', null, $product_id); $language_code = isset($lang['language_code']) ? $lang['language_code'] : ''; return [ 'object_id' => $object_id, 'variation_id' => $variation_id, 'title' => $title, 'content' => $content, 'sku' => $sku, 'search_terms' => $search_terms, 'lang' => $language_code, 'status' => 'publish' ]; } /** * Sanitizes raw text into clean search tokens, removing stop words and applying synonyms. */ private function sanitize_and_tokenize($string) { $settings = get_option('awas_settings', []); return \Awas\AdvancedSearch\Core\Helper::sanitize_and_tokenize($string, $settings); } }