Support » Plugin: WooCommerce » First page of search results not displayed (but pages 2+ are fine..?)

  • Resolved Flamekebab

    (@flamekebab)


    I’m using a rather ancient theme but I’m not sure how that affects matters.
    I was having some issues until I discovered that I had to declare WooCommerce support for things to work. I’ve now done that and now I have different issues. Yay!

    As the title says my product search seems a bit broken. It now loads properly ish on the second page of results:

    The first page is just blank as you can see. I’m not sure why products are shown in groups of four either (my shop is configured to show rows of three).

    Any ideas on where to start debugging this?

    • This topic was modified 1 year, 8 months ago by Flamekebab. Reason: Got my image syntax wrong!
Viewing 3 replies - 1 through 3 (of 3 total)
  • Further work on this issue has revealed a likely cause of the problem:

    hidden items aren’t removed properly.

    I have various items that are out of stock and the option to hide them is enabled (Hide out of stock items from the catalog for those of you in the audience googling).

    For a start those items are not being hidden from $total (and this applies when visiting this site as a user, not an admin too). This leads to an incorrect search/catalogue result count:

    Furthermore it leads to items being removed page by page – not from the complete query.

    For example if I do a search that should get 30 results and there are only 26 visible products strange things happen when the site is set to show 10 per page.
    The first page will show 6 products, the second 10, the third 10. Or maybe it’ll be 9 on the first page, 7 on the second, and 10 on the last (depending on where in the array the products are marked as hidden, presumably).

    How do I fix this?

    I was eventually able to determine what was happening – for whatever reason WooCommerce is not handling the filtering of a $query correctly.

    This applies to searches, listing by tag, listing by categories, and probably other things too.

    My shop is set to hide items that are out of stock and it does – but they’re not being correctly filtered out. I hacked together this fix and added it to the bottom of my theme’s functions.php:

    function fox_filter_query_results($query) {
                
    //We need a conditional too to make sure weird stuff doesn't happen.
    if($query->is_search || $query->is_tax){    
    // only deal with the 'product' post type
                    $query->set('post_type', array('product', 'post'));
    
    //Only show results that are in stock. 
                    $query->set('meta_query', array( array(
                'key' => '_stock_status',
                'value' => 'instock',)) );
    }
            return $query;
    }
    add_filter('pre_get_posts','fox_filter_query_results');

    This fixed the problem. The total for search results and other things that use $query are now correctly pruned and a page of 30 products is 30 products long.

    Oddly enough I couldn’t get the query to behave with regards to checking whether a product category or tag was being viewed but both of those queries have is_tax() returning true so I went with that as the conditional instead. If someone could give me a better line of code to replace that though I’d be pleased as it feels a little hacky (given that it’s a check for taxonomy in a way I don’t really understand). Then again product_category and product_tag are both custom taxonomy so maybe that’s what it’s for?

    Updated code that doesn’t rely on is_tax:

    //Queries aren't respecting that out of stock items aren't to be shown - this affects search and the product catalogue:
    function fox_filter_query_results($query) {
    
    //Retrieve the query object using its method? Class? I'm in terminology hell right now:
    $queryObject = $query->get_queried_object();
    
    //We need a conditional too to make sure weird stuff doesn't happen.
    //The query object can either be 'product' or it's a taxonomy thing in which case we want the WooCommerce taxonomies:
    if($queryObject->name == "product" || $queryObject->taxonomy == "product_cat"|| $queryObject->taxonomy == "product_tag"){
                    // only search the product post type
                    $query->set('post_type', array('product', 'post'));
    
    //in principle this should only show results that are in stock. 
                    $query->set('meta_query', array( array(
                'key' => '_stock_status',
                'value' => 'instock',)) );
    }
            return $query;
    }
    add_filter('pre_get_posts','fox_filter_query_results');
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘First page of search results not displayed (but pages 2+ are fine..?)’ is closed to new replies.