Skip to content

woocommerce_redirect_single_search_result not working with /search url #36043

@Konamiman

Description

@Konamiman

Prerequisites

  • I have carried out troubleshooting steps and I believe I have found a bug.
  • I have searched for similar bugs in both open and closed issues and cannot find a duplicate.

Describe the bug

When doing a search that results in one single result one would expect that when the woocommerce_redirect_single_search_result is set to return false, the regular search results page would appear, instead of the single product details page. This is indeed the case when doing the search via ?s=<term> endpoint, but it's not (the filter is ignored) when searching via /search/<term>.

The code causing this is in the wc_template_redirect function in the wc-template-functions.php file. Turns out that is_post_type_archive( 'product' ) is returning false for searches made via /search.

Expected behavior

The woocommerce_redirect_single_search_result filter behaves as expected for any kind of search.

Actual behavior

The woocommerce_redirect_single_search_result filter doesn't work when the search is made via /search/<term>.

Steps to reproduce

  1. Make sure that you have a product with a unique name in your shop, for example "fizzbuzzer".
  2. Add this snippet somewhere: add_filter('woocommerce_redirect_single_search_result','__return_false');
  3. Go to the following route: yoursite?s=fizzbuzzer, the search results page will display.
  4. Go to the following route: yoursite/search/fizzbuzzer, the search results page should display but instead the product page appears.

WordPress Environment

WooCommerce 7.2

Isolating the problem

  • I have deactivated other plugins and confirmed this bug occurs when only WooCommerce plugin is active.
  • This bug happens with a default WordPress theme active, or Storefront.
  • I can reproduce this bug consistently using the steps above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugThe issue is a confirmed bug.KirigamiWC Store Editing (FSE)Store FrontendIssues related to shop page.plugin: woocommerceIssues related to the WooCommerce Core plugin.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions