• Resolved perrymediazevenbergen

    (@perrymediazevenbergen)


    Hi there,

    I’m looking for a way to hide products of a specific category (mut-haves) on the shop page. I don ‘t want to hide them in my entire shop, only on the front page of my shop.

    I made an simple image of what I want.
    https://cloud.perrymedia.nl/index.php/s/Vku3UREhU0jfcaZ
    password ‘perrymedia’

    This option will hide the products in my entire shop, even when you select the category.

    /**
     * Exclude products from a particular category on the shop page
     */
    function custom_pre_get_posts_query( $q ) {
    
        $tax_query = (array) $q->get( 'tax_query' );
    
        $tax_query[] = array(
               'taxonomy' => 'product_cat',
               'field' => 'slug',
               'terms' => array( 'musthaves' ), // Don't display products with category "musthave" on the shop.
               'operator' => 'NOT IN'
        );
    
        $q->set( 'tax_query', $tax_query );
    
    }
    add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );  

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • jessepearson

    (@jessepearson)

    Automattic Happiness Engineer

    @perrymediazevenbergen It appears you have a very similar snippet to what is on this page:
    https://wpbeaches.com/hide-woocommerce-product-categories-shop-page/

    There is one part that is missing though. Line 11 of the code on the link above has an if statement that is needed to be able to define the page(s) you’d like to hide your category or categories on. Right now your snippet targets every page.

    Thread Starter perrymediazevenbergen

    (@perrymediazevenbergen)

    Hi Jesse,

    Thanks for your support. This is indeed what i’m looking for. But it doesn’t work properly.

    I’ve tried the following code. You have any suggestions? (I don’t cache pages inside my shop)

    add_action( 'woocommerce_product_query', 'prefix_custom_pre_get_posts_query' );
    /**
     * Hide Product Cateories from targetted pages in WooCommerce
     * @link https://gist.github.com/stuartduff/bd149e81d80291a16d4d3968e68eb9f8#file-wc-exclude-product-category-from-shop-page-php
     *
     */
    function prefix_custom_pre_get_posts_query( $q ) {
    	
    	if( is_shop() || is_page('shop') ) { // set conditions here
    	    $tax_query = (array) $q->get( 'tax_query' );
    	
    	    $tax_query[] = array(
    	           'taxonomy' => 'product_cat',
    	           'field'    => 'slug',
    	           'terms'    => array( 'musthave' ), // set product categories here
    	           'operator' => 'NOT IN'
    	    );
    	
    	
    	    $q->set( 'tax_query', $tax_query );
    	}
    }
    
    jessepearson

    (@jessepearson)

    Automattic Happiness Engineer

    @perrymediazevenbergen I am unsure, because I added the code to my store and it works correctly. This can be seen here: https://cld.wthms.co/gp49Za

    Thread Starter perrymediazevenbergen

    (@perrymediazevenbergen)

    @jessepearson You’re right! I have been so stupid to upload the wrong functions.php file to my FTP. That’s the reason why it didn’t worked.

    Thanks for the solution!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Hide products from shop page but not entire shop’ is closed to new replies.