Remove WooCommerce Products From Main Shop Page/ Integrity

Hi!

This post is somewhat in reference to my previous thread https://theme.co/apex/forum/t/remove-products-from-woocommerce-shop-pages-integrity/47392 However, I have a partial solution. but need help adjusting the code so that it works for all products.

Without this code, any WooCommerce product that is created appears on the main shop page. I do not wish to have them appear in this way because I am also using Envira gallery and would like certain items to appear in certain areas only on specific pages.

With the code added to the functions file the separate product image does not appear. Here is the code…

/**
 * 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( 'melanin crown' ), // Don't display products in the hair products category on the shop page.
           'operator' => 'NOT IN'
    );


    $q->set( 'tax_query', $tax_query );

}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' ); 


add_filter( 'wc_stripe_show_payment_request_on_checkout', '__return_true' );

The website is not live yet so I will include login details in a secure note.

Hi @fantasy_5,

Thanks for reaching out.

I think you got your code from here https://wordpress.org/support/topic/hide-products-from-shop-page-but-not-entire-shop/, as per the discussion, what you’re using is not working due to missing if. You should implement the working copy at the end of the discussion.

And this part of your code is wrong too,

array( 'melanin crown' )

it should be

array( 'melanin', 'crown' )

Thanks!

Hi, Rad

The code does work and it has successfully removed the woo product under the melanin crown category from the shop page. What I need to know is how to adjust it so that it removes all woo category products from the shop page. I am using envira gallery so i will have product appear on specific pages. I do not need duplicates on the main shop page. Here is the code again.

/**

  • 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( ‘melanin crown’ ), // Don’t display products in the hair products category on the shop page.
    ‘operator’ => ‘NOT IN’
    );

    $q->set( ‘tax_query’, $tax_query );

}
add_action( ‘woocommerce_product_query’, ‘custom_pre_get_posts_query’ );

add_filter( ‘wc_stripe_show_payment_request_on_checkout’, ‘__return_true’ );

Nothing from this particular category appears on the shop page only the designated page while this code is active. Please see the attachment and compare it to the first one.

Hi @fantasy_5,

What do you mean all category products? Is it the category filtered products, or category display? If it’s about category being displayed on a shop page, then please go to Admin > Appearance > Customizer > Woocommerce > Product Catalog > Shop page display and make sure it only display products and not category.

If it’s still product filtering, I assume the 'melanin crown' is a single category instead of two, then please change it to ‘melanin-crown’ since 'field' => 'slug'.

I recommend contacting the author of the code as well.

Thanks!

Hi, Rad

I don’t want products or categories to display on the shop page because I am using Envira gallery. I am trying to avoid duplicates. Certain items will appear on certain pages for my store. Can you please login to my website so you can get a better idea of what I’m trying to accomplish.

Hi @fantasy_5,

Yes, then I was correct. I just thought what I’m referring was wrong as you’re providing the same code while removes all woo category products from the shop page. I was just confused a bit with terminology :slight_smile: , sorry about that.

And I can’t check your products as every time I access it, it redirects me here

Thanks!

Hi, Rad!

Yes, there was a redirect to the license page issue initially because I had to download a different version of the Alidropshipwoo plugin. The one I had wasn’t compatible with Siteground. However, it has been resolved.

You can view a full list of my products and product categories under the product tab in my admin panel. I’m new to Woocommerce and still trying to understand certain settings. This link helped me put categories on a specific page. Unfortunately, everything I add to the store will end up on the shop page not organized in a way I’d like. https://atlantisthemes.com/woocommerce-categories/

For instance, this shortcode helps me put all items that fall under the category of women on that particular page.

[product_category category=”women” orderby=”date”]

I wonder if there’s a shortcode that will exclude all products/ categories from the shop page? Hmmm…

Hi,

You can try using term_operator="NOT IN"

eg.

[products columns="3" attribute="season" terms="warm" terms_operator="NOT IN"]

Please refer to the link below for more information.

Thanks

Hi, Paul

In reference to the code I have above… I do notice it includes the operator “not in” function. Since it work to move a single product from a particular category on the shop page how can I add multiple products to it so that it works for all. I tried the short code above, but it didn’t do the trick. I’m not really sure how to implement it.

Update: So I figured out how to remove most of the product display, etc, from the shop page using this CSS

.entry-product {display:none}

.woocommerce-result-count {display:none}

.orderby {display:none}

.pre {display:none}

div.woocommerce.columns-4 {display:none}

.woocommerce-notices-wrapper {display:none}

The only thing is it removes the woocommerce items from all pages. I tried removing them via page id, but it didn’t do the trick. What am I missing here?

Example: .page-id-1258 .entry-product {display:none}

Hi @fantasy_5,

If you would like to hide the products using custom CSS. Please try with this:

.woocommerce li.product.post-123,
.woocommerce li.product.post-456 {
    display: none;
}

The 123 and 456 number is the product ID.

Hope it helps :slight_smile:

Hi, Thai

The product code would probably work if I were only adding a few products to the store. Let’s say, for instance, I added 100 then I would need to use the code for every single product id. What I wish to accomplish is not displaying Woocommerce products by default on the shop page every time a product is added to my store.

Yes, I would like to hide elements using CSS but only on the shop page and not have the CSS affect any other page. The page id for the shop page is 1258. Example: .page-id-1258

This code removes all elements and products pertaining to Woocommerce however, in implementing this CSS it also removes all products under the women’s page. How do I target the shop page in particular to remove these items? Can you please log into my website so you can better understand what I’m trying to accomplish?

Blockquote
.entry-featured {display:none}
.entry-product {display:none}
.orderby {display:none}
.woocommerce-result-count {display:none}
.woocommerce columns-4 {display:none}
.products cols-4 {display:none}
.woocommerce-notices-wrapper {display:none}
div.woocommerce.columns-4 {display:none}

The only element I was unable to move via css is below. Please see attachment.

Hi @fantasy_5,

You can add a unique body class to your shop page so that you can target it using css.

To do that, add the code below in your child theme’s functions.php file

add_filter( 'body_class','my_body_classes' );
function my_body_classes( $classes ) {
 
    if ( is_shop() ) {
     
        $classes[] = 'my-shop-page';    
         
    }
     
    return $classes;
     
}

You can then target your shop page using that class

For example

.my-shop-page div.woocommerce.columns-4 {
     display:none;
}

To remove the extra space, add this in Theme Options > CSS

.my-shop-page pre {
     display:none;
}

Hope this helps

1 Like

Hi, Paul!

Thanks so much! You’re one smart cookie. I was going bananas trying to figure things out. :confused: I added the code you suggested to the functions file then edited all of the necessary CSS above like so…,

Blockquote
.my-shop-page .entry-featured {display:none}

As a result, none of the items on the women’s page have disappeared. Thanks again.

You’re most welcome!

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.