Zed0121
Zed0121

Reputation: 157

How to disable/hide woocommerce category page?

I used the following code to hide the single product pages on woocommerce, which worked perfectly. Anyone trying to access single product pages are redirected to the home page.

I now want to hide the category pages. I dont need these as I am using the category shortcode to display product on other pages. Can anyone help with the required code?

//Removes links
add_filter( 'woocommerce_product_is_visible','product_invisible');
function product_invisible(){
    return false;
}

//Remove single page
add_filter( 'woocommerce_register_post_type_product','hide_product_page',12,1);
function hide_product_page($args){
    $args["publicly_queryable"]=false;
    $args["public"]=false;
    return $args;
}

Taken from: How to disable/hide woocommerce single product page?

Upvotes: 5

Views: 9343

Answers (3)

Niket Joshi
Niket Joshi

Reputation: 738

add_action( 'woocommerce_product_query', 'bbloomer_hide_products_category_shop' );

function bbloomer_hide_products_category_shop( $q ) {

  $tax_query = (array) $q->get( 'tax_query' );

  $tax_query[] = array(
         'taxonomy' => 'product_cat',
         'field' => 'slug',
         'terms' => array( 'chairs' ), // Category slug here
         'operator' => 'NOT IN'
  );


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

}

please check this example.

Upvotes: 0

kcpr
kcpr

Reputation: 1105

If You'd like to possibly fully "hide" the page and display "Page not found" (404 error) page, You can add the following into Your "functions.php" file:

function display_404_page_instead_of_products_category_page() {
  if ( is_product_category() ) {
    global $wp_query;
    $wp_query->set_404();
    status_header(404);
  }
}
add_action( 'wp', 'display_404_page_instead_of_products_category_page' );

If You'd also like to hide a products tag page, just modify the condition accordingly:

if ( is_product_category() || is_product_tag() ) {
...
}

(You can also hide - for example - an image browser page if You wish, just use is_attachment() function in that case.)

Thank You @LoicTheAztec for letting me know of "is_product_category" function.

Upvotes: 0

LoicTheAztec
LoicTheAztec

Reputation: 254378

You could try to use this custom function, that will redirect to shop page, when a product category archive page is called:

add_action( 'template_redirect', 'wc_redirect_to_shop');
function wc_redirect_to_shop() {
    // Only on product category archive pages (redirect to shop)
    if ( is_product_category() ) {
        wp_redirect( wc_get_page_permalink( 'shop' ) );
        exit();
    }
}

Code goes in function.php file of your active child theme (or active theme) or in any plugin file.

Tested and works

As I don't think that you want to disable product categories functionality, but just the related archive page…

Upvotes: 8

Related Questions