• Resolved Henrik

    (@henbik)


    Hi Fibo, first of all thank you so much for the great search engine you have created.

    I need assistance in creating a code that gives products with certain words in the name a lower score. Products that have “B-vare”, “C-vare” and “Angrerett” in the title, must be displayed after the other items, while today they are displayed first.

    Can anyone of you assist me with this?

    Best regards,
    Henrik

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Kris

    (@c0nst)

    Hi @henbik

    Here, try this code snippet:

    add_filter( 'dgwt/wcas/search_results/product/score', function ( $score, $keyword, $post ) {	
    	$product_title = get_the_title( $post );
    	$phrases       = [ 'B-vare', 'C-vare', 'Angrerett' ];
    	
    	foreach ( $phrases as $key => $phrase ) {
    		if ( strstr( $product_title, $phrase ) !== false ) {
    			$score -= 50;
    		}
    	}
    				
        return $score;
    }, 10, 3 );

    It will decrease the product suggestion score by 50 if the phrase from $phrases is contained in the product title. You can increase this value if the results are still too high.

    You have two ways to add this code to your theme:
    Open the functions.php in your child theme and add the code at the end.
    or install the Code Snippets plugin and apply this code as a snippet.

    Regards,
    Kris

    Thread Starter Henrik

    (@henbik)

    Fantastic, Kris! That worked great.

    Thank you very much!

    Best regards,
    Henrik

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add code to give some items a lower rating’ is closed to new replies.