I am trying to hide the single product detail page on my wordpress-woocommerce site. How can i achieve this without breaking woocommerce functionality?
The single page is something that is provided from WordPress and there is no way to disabled it. But there some ways to prevent access to single product page.
The first one is to edit your shop (products-archive) template and to delete all the places where you have a link to the single page.
The second is to do a check on each page load if the page is a single product page and redirect the user to wherever you want:
add_action('init','prevent_access_to_product_page');
function prevent_access_to_product_page(){
if ( is_product() ) {
wp_redirect( site_url() );//will redirect to home page
}
}
You can include this code in your functions.php file of your child-themes directory. Have in mind that I've haven't tested the code.
remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
You can register a hook that returns 404 in case of product pages using is_product()
helper function
function prevent_access_to_product_page(){
global $post;
if ( is_product() ) {
global $wp_query;
$wp_query->set_404();
status_header(404);
}
}
add_action('wp','prevent_access_to_product_page');
Solution is tested and working.
Note: solution was based somehow on some info from @ale's answer.
You can remove the anchor generated on shop page which would never redirect user to single page. For that, you've to paste this code in your functions.php file.
remove_action(
'woocommerce_before_shop_loop_item',
'woocommerce_template_loop_product_link_open',
10
);
This code will remove link but, after that you've to remove anchor closing tag as well just it doesn't break your html
remove_action(
'woocommerce_after_shop_loop_item',
'woocommerce_template_loop_product_link_close',
5
);
Put it in functions.php
//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;
}