• Resolved stojanclover75

    (@stojanclover75)


    Is there a way for a Free plugin option to restrict search to few categories, and ignore others? or give very high priority to search results if products belong to those categories and show very low in the list products from other categories?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi @stojanclover75

    Here is the the code that will exclude categories from the search results:

    add_filter( 'dgwt/wcas/search/product_cat/args', function ( $args ){
        $args['exclude'] = array( 999, 1000, 1001 ); // The ID of categories to exclude
        return $args;
    } );

    Here is a code that allows you to add extra score to selected categories:

    // Add extra score to products that are assigned to the selected category
    add_filter( 'dgwt/wcas/search_results/product/score', function( $score, $keyword, $product_id ) {
    	$terms = array( 999, 1000, 1001 ); // The ID of categories to promote
    
    	if ( $terms ) {
    		foreach ( $terms as $term ) {
    			if ( has_term( $term, 'product_cat', $product_id ) ) {
    					$score += 100;
    			}
    		}
    	}
    
    	return $score;
    }, 10, 3 );
    

    Implementation:

    1. Open the functions.php file in your child theme and add the code at the end
    2. Or install the Code Snippets plugin and apply this code as a snippet.

    Regards,
    Kris

    Thread Starter stojanclover75

    (@stojanclover75)

    Hi @c0nst

    thank you for your fast reply.

    Code you provided works like a charm ??

    Helped me a lot! Thanks! ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Restrict search to few product categories’ is closed to new replies.