Home » WooCommerce: Redirect Specific Product Search To Custom URL

WooCommerce: Redirect Specific Product Search To Custom URL

by Tutor Aspire

Go to a WooCommerce website. Search for a product. Get to the search results page, which displays from 0 to N products based on the search term, sorted by relevancy. Easy.

Now, let’s imagine you have a custom landing page for “Tables”, and you want people searching for “tables” to go to that page instead of the default search result page. Quite easy as well – thanks to a neat PHP snippet we will feature today.

Enjoy!

When searching for “tables”, instead of the default search results page, I want WooCommerce to redirect the customer to a different URL, maybe because I have a special landing/sales page for customers searching for tables.

PHP Snippet: Redirect Specific Search Term Result to a Custom URL @ WooCommerce Frontend

/**
 * @snippet       Redirect WooCommerce Search Result Page
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 7
 * @donate $9     https://www.tutoraspire.com
 */
 
add_action( 'template_redirect', 'tutoraspire_redirect_search_results' );

function tutoraspire_redirect_search_results() {
    if ( isset( $_GET['s'] ) && strcasecmp( $_GET['s'], 'tables' ) == 0 ) {
        wp_redirect( 'https://example.com' );
        exit();
    }
}

You may also like