How to disable/hide woocommerce category page?
Asked Answered
R

3

5

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?

Raviv answered 30/11, 2017 at 16:24 Comment(2)
Where is your HTML ?Boyhood
@BASEERHAIDER HTML is not needed for this. This is a PHP issue.Wharf
J
8

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…

Jos answered 30/11, 2017 at 16:50 Comment(0)
D
0
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.

Dice answered 2/12, 2017 at 19:28 Comment(0)
L
0

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.

Lorrainelorrayne answered 27/6, 2021 at 15:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.