Partial search not working for product variation SKUS
-
Hello,
I am using Flatsome Version 3.14.1 and I have activated partial search and added _sku to the custom fields for indexing.
The problem is that the partial search works for the skus of the main product but not for the skus of the product variations.
Product numbers of main products look like 00000238 and product numbers of variations look like 00000354, 0000354-R or 00000353-R-2-Mini. The search doesn’t work no matter if the product number is only a number or if there’s and addition with “-” und/or text.
Could you help me out with this one? Thank you.
I have also added this code to the functions.php:
remove_action( 'woocommerce_after_main_content', 'flatsome_pages_in_search_results', 10 ); // Add Pages and blog posts to top of search results if set. function relevanssi_pages_in_search_results() { if ( ! is_search() || ! get_theme_mod( 'search_result', 1 ) ) { return; } global $post; if ( get_search_query() ) { $args = array( 'post_type' => 'post', 's' => get_search_query(), 'relevanssi' => true, ); $query = new WP_Query( $args ); $posts = array(); while ( $query->have_posts() ) { $query->the_post(); array_push( $posts, $post->ID ); } $args = array( 'post_type' => 'page', 's' => get_search_query(), 'relevanssi' => true, ); $query = new WP_Query( $args ); $pages = array(); while ( $query->have_posts() ) { $query->the_post(); $wc_page = false; if ( 'page' === $post->post_type ) { foreach ( array( 'shop', 'cart', 'checkout', 'view_order', 'terms' ) as $wc_page_type ) { if ( $post->ID === wc_get_page_id( $wc_page_type ) ) { $wc_page = true; } } } if ( ! $wc_page ) { array_push( $pages, $post->ID ); } } do_action( 'flatsome_products_page_loader' ); if ( ! empty( $posts ) || ! empty( $pages ) ) { $list_type = get_theme_mod( 'search_result_style', 'slider' ); if ( ! empty( $posts ) ) { echo '<hr><h4 class="uppercase">' . __( 'Posts found', 'flatsome' ) . '</h4> ' . do_shortcode( '[blog_posts columns="3" columns__md="3" columns__sm="2" type="' . $list_type . '" image_height="16-9" ids="' . implode( ',', $posts ) . '"]' ); } if ( ! empty( $pages ) ) { echo '<hr><h4 class="uppercase">' . __( 'Pages found', 'flatsome' ) . '</h4> ' . do_shortcode( '[ux_pages columns="3" columns__md="3" columns__sm="2" type="' . $list_type . '" image_height="16-9" ids="' . implode( ',', $pages ) . '"]' ); } } } } add_action( 'woocommerce_after_main_content', 'relevanssi_pages_in_search_results', 10 ); /** * Replaces the Flatsome live search default function (get_posts()) with Relevanssi. * * @param string $query The search query, not used. * @param array $args The search query arguments. * @param array $defaults The default values, not used. * * @return array An array of post objects. */ function rlv_flatsome_search_function( $query, $args, $defaults ) { $_wp_query = new WP_Query(); $_wp_query->parse_query( $args ); relevanssi_do_query( $_wp_query ); return $_wp_query->posts; } add_filter( 'flatsome_ajax_search_function', function() { return 'rlv_flatsome_search_function'; } ); add_filter( 'post_type_link', 'rlv_wc_variation_permalink', 10, 2 ); function rlv_wc_variation_permalink( $permalink, $post ) { global $flatsome_live_search_query, $wp_query; if ( empty( $flatsome_live_search_query ) && isset( $wp_query->query_vars['s'] ) ) { $flatsome_live_search_query = $wp_query->query_vars['s']; } if ( 'product' === $post->post_type && ! empty( $flatsome_live_search_query ) ) { $variations = rlv_get_variation_ids( $post->ID ); if ( isset( $variations[ $flatsome_live_search_query ] ) ) { remove_filter( 'post_type_link', 'rlv_wc_variation_permalink', 10 ); return get_permalink( $variations[ $flatsome_live_search_query ] ); } } return $permalink; } function rlv_get_variation_ids( $product_id ) { $variation_ids = array(); $args = array( 'post_parent' => $product_id, 'post_type' => 'product_variation', 'posts_per_page' => -1 ); $variations = get_posts( $args ); if ( ! empty( $variations ) ) { foreach ( $variations as $variation ) { $variation_ids[ strtolower( get_post_meta( $variation->ID, '_sku', true ) ) ] = $variation->ID; } } return $variation_ids; }
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Partial search not working for product variation SKUS’ is closed to new replies.