• Resolved creativ3y3

    (@creativ3y3)


    I am trying to limit the number of products showing in the shop by looking up an ACF field on the product to see if it has a value of 1 and using pre_get_posts to filter them out. It all works as expected in the shop, however, when it comes to using filters on the shop page (like price or categories), it brings back all products and categories associated with all products (even the ones I’ve filtered out) so if someone then uses those filters, it shows the products I don’t want to see again. Does anyone have any suggestions how I could overcome this issue? My code looks like this so far:

    add_action( ‘pre_get_posts’, ‘custom_pre_get_posts_query’ );
    function custom_pre_get_posts_query( $q ) {
    
        if ( ! $q->is_main_query() ) return;
        if ( ! $q->is_post_type_archive() ) return;
        
        if ( ! is_admin() && is_shop() ) {
    
          $hideProducts = array();
        
          $posts = get_posts(array(
            'numberposts' => -1,
            'post_type'   => 'product',
            'meta_query'  => array(
              'relation'    => 'AND',
              array(
                'key'     => 'sell_product_online',
                'value'     => '1',
                'compare'   => '=',
              ),
            ),
          ));
          
          foreach ($posts as $another) {
                  array_push($hideProducts, $another->ID);
              }
          $q->set( 'post__not_in', $hideProducts ); 
        }
    
        remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
    
      }

    Any help appreciated.

Viewing 8 replies - 1 through 8 (of 8 total)
  • I may be missing the point, but why not just set the products’ “Catalog Visibility” to “Hidden,” which will keep them from appearing in Shop and Search results?

    Thread Starter creativ3y3

    (@creativ3y3)

    @linux4me2 Thanks for getting back and for the suggestion, sadly that gave me a couple of other issues:
    1 – I use sliders built by visual composer which search for category’s of product and show them as related to each other. I still want them to show in these instances. But they don’t sadly- it site wide hides them.
    2 – I wasn’t to sure on the functionality of itself ie, if it would hide the product from search engines or not?
    3. It was hiding the product from the website search, I still need it to be seen on other parts / pages / website search pages, just not be able to buy it or show it in the shop.
    4. I have a discontinued section for products filtered by category, I’d still need it to show in there if it was ever discontinued.

    I’d also still need to hang it around some conditions as they all have to be hidden based on a “approved” geo location too. So hide all the ecommerce bits, like cart, add to cart buttons etc. If they are in an “approved” location, show all these things – but hide the “hidden products” from the shop front (and shop filters) and hide the add to cart button on those too – while still showing it on sliders and website search too etc.

    Maybe there is ways around these issues that I didn’t think of though?

    Alternatively maybe another route I could go thinking about it is not use the standard shop filter by category widgets etc but instead add my own filters, and use hrefs / onlcick events etc to add “?product_tag=mytag” for example to the url as it filters down the correct products.

    I think I’m with you. So your code above works except when a user uses the filters?

    What happens when you comment out the line:

    
    if ( ! $q->is_main_query() ) return;
    

    I’m wondering if the filters return false on the main query check. It seems like there will be a way to execute your code for the query the filters run as well.

    Thread Starter creativ3y3

    (@creativ3y3)

    Unfortunately that breaks the query. For the most part I can rebuild the filters I think. The one I’m struggling with is the price filter. It seems to bring back the “most expensive” product price for the max price in the entire product range, rather than the most expensive price in the custom_pre_get_posts_query.

    So the min price product over all is £50 and max £1000 and this is what it has set in the min max on the slider. However on the custom query the min price is £50 and Max £500. So I was hoping it would show £500 instead of £1000. The good thing is that it doesnt actually show the more expensive products in the results even if you filter up to £1000 – just the label that is wrong.

    Is there a way to hook the price filter to give it the max price of the custom query even?

    Have you seen this post?

    It seems like there should be an action that would govern both the initial display in the shop page and the filters. Maybe the action in the accepted answer, “woocommerce_product_query_meta_query,” might do it? It looks like it would alter any query passed to the shop page, and it’s a pretty simple way to handle your “sell_product_online” meta key.

    Thread Starter creativ3y3

    (@creativ3y3)

    @linux4me2 you are a legend! I hadn’t seen that post. Just quickly tried it, will have a better look tomorrow. Strangely it doesn’t appear to filter out the categories, so may have to deal with that issue separately. It does seem odd as you say that there is no “catch all”, or at least none that I have found yet. Your solution does however appear to change the price on the slider to the correct value – hopefully solves that part, which was turning out to be the biggest headache!

    Thanks for your help, much appreciated.

    @creativ3y3 Let me know what you find out.

    Since the categories are a taxonomy, there’s a “woocommerce_product_query_tax_query” filter you may be able to use in conjunction with the above to limit the categories listed in the drop-down filter, and limit it to the Shop page.

    corsonr

    (@corsonr)

    Automattic Happiness Engineer

    We haven’t heard back from you in a while, so I’m going to mark this as resolved – if you have any further questions, you can start a new thread.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Limit product results in WooCommerce shop page’ is closed to new replies.