Skip to content
This repository has been archived by the owner on Sep 16, 2019. It is now read-only.

WooCommerce 3.0 - Hidden products showing in search results #1068

Closed
newtlabs opened this issue Sep 6, 2017 · 4 comments
Closed

WooCommerce 3.0 - Hidden products showing in search results #1068

newtlabs opened this issue Sep 6, 2017 · 4 comments

Comments

@newtlabs
Copy link

newtlabs commented Sep 6, 2017

How can this bug be reproduced?

  1. Step one - add a product as hidden
  2. Step two - search for product
  3. Step three - see hidden product shown in search results

What did you expect to happen?

I expected to see no hidden products shown in search result

What happened instead?

I saw hidden products

  • OS & version: n/a
  • Node version (node -v) [Node v4+ is required] : latest
  • FoundationPress version (see line 3 in package.json): 2.7.1
  • Foundation version (see dependencies in package.json) : n/a

The search needs changing, heres what I'm currently using as a custom function to get around this issue for now until I have more time or when someone else can fix:

/* Prevent Hidden WooCommerce products from showing up in WordPress search results */
if ( ! function_exists( 'hidden_search_query_fix' ) ) {
  function hidden_search_query_fix( $query = false ) {
    if ( ! is_admin() && is_search() ) {
      $query->set( 'tax_query', array(
      'relation' => 'OR',
      array(
        'taxonomy' => 'product_visibility',
        'field'    => 'name',
        'terms'    => 'exclude-from-catalog',
        'operator' => 'NOT IN',
      ),
      array(
        'taxonomy' => 'product_visibility',
        'field'    => 'name',
        'terms'    => 'exclude-from-catalog',
        'operator' => '!=',
      ),
      ));
    }
  }
}
add_action( 'pre_get_posts', 'hidden_search_query_fix' );

Cheers!

@colin-marshall
Copy link
Collaborator

colin-marshall commented Sep 6, 2017

Apparently this is always an issue when you return WooCommerce products in a search outside of the WooCommerce loop. This is because the "hidden" status of a post is unique to WooCommerce and is not recognized in WordPress core.

@newtlabs what do we need to improve on in your custom function to make it apart of FoundationPress?

On a semi-relevant note, we could start putting all WooCommerce related functions in a new file at /library/woocommerce-support.php. We could wrap all of the functions in that file a conditional that checks if WooCommerce is activated so that they are only loaded when the site is using WooCommerce. The special WooCommerce sidebar I suggested in #1057 could also go in this file so it doesn't appear if WooCommerce is not installed and activated.

@ghost
Copy link

ghost commented Sep 7, 2017

@colin-marshall, the _s theme takes this approach: https://github.com/Automattic/_s/blob/master/inc/woocommerce.php

@olefredrik
Copy link
Owner

Is this still an issue?

@colin-marshall
Copy link
Collaborator

colin-marshall commented Oct 2, 2017

@olefredrik after thinking about this further, I don't think this change should be added to the framework. I don't see any similar functionality in starter theme _s, or even in the "full-blown" Storefront theme, which is the official WooCommerce theme.

Further, modifying the search query will degrade performance. Source: woocommerce/woocommerce#12308 (comment)

Since a small percentage of people using FoundationPress will also use WooCommerce, it would degrade search query performance with zero benefit for the majority of our users. The best solution for us is to exclude it from FoundationPress and advise people to use search query modifying plugins or add their own function to a project.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants