WooCommerce: Redirect Specific Product Search To Custom URL

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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 7
 * @community     https://businessbloomer.com/club/
 */
 
add_action( 'template_redirect', 'bbloomer_redirect_search_results' );

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

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *