Search code examples
phpwordpresswoocommercehttp-redirectcategories

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?


Solution

  • 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…