Product Variations Searchable but Show only Parent Products as Results

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Product Variations Searchable but Show only Parent Products as Results

This topic contains 3 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 1 month ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #37550
    niumalta96
    niumalta96
    Participant

    We have a woocommerce site and we need to have the Product Variations searchable on our site, however without showing the in the search results page and show only the Parent Product instead.

    Do you know if there is a way to do this implementation? We have developers available and if need be we can hook with filters and hooks in order to modify the default’s plugin logic.

    Attachments:
    You must be logged in to view attached files.
    #37562
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi!

    I believe it is possible, and the only way is to use the index table engine, along with a custom code snippet.

    First, make sure to configure the index table engine, only to index the product post type (and everything else you may want to search, except the product variations).
    Then using this custom code, to append the variation information to the actual product:

    add_filter( 'asp_post_content_before_tokenize_clear', 'asp_tokenize_sku_variation', 10, 2 );
    function asp_tokenize_sku_variation($content, $post) {
      if ( $post->post_type == 'product' && ($variations = $_product->get_children()) ) {
    	foreach ( $variations as $variation ) {
    		$content .= ' ' . $variation->get_description();
    		$content .= ' ' .  $variation->get_sku();
    	}
      }
    
      return $content;
    }

    I have not been able to test this code for now. It should get all the variations for each product during indexing, and then append their SKU and descriptions to the main product. So searching variation data should yield the parent product as the result.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #37568
    niumalta96
    niumalta96
    Participant

    Thank you Ernest. In the meantime our developers tried with the below and it seems they got the desired result. I will keep a note of your suggestion in case they notice any issues with our approach. Thank you!

    add_filter( 'asp_pagepost_results', __CLASS__ . '::remove_variants_products', 1, 1 );

     public static function remove_variants_products( $pageposts ) {
       foreach ( $pageposts as $post_key => $pagepost ) {
           if( $pagepost->post_type == 'product_variation' ) {
               unset( $pageposts[$post_key] );
           }
       }
    
       return $pageposts;
     }
    #37583
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Great! Let me know if you need any more help 🙂

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.