I am trying to hide the single product detail page on my wordpress-woocommerce site. How can i achieve this without breaking woocommerce functionality?
You can remove the anchor generated on shop page which would never redirect user to single page. For that, you have 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 have 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;
}
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.
The single page is something that is provided from WordPress and there is no way to disable it. But there are some ways to prevent access to single product pages.
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-theme's directory. Have in mind that I haven't tested the code.
In most cases, removing the possibility to click on the link to product page will effectively "disable" going to the product page. You can do it with CSS (adjust classes for your theme):
.product-title a,
.product-container a,
.product-details a {
pointer-events: none;
}
NOTE: this will not remove the page itself, but disable clicking to get to it. To disable accessing pages via direct link, use some of the offered redirect solutions.
It may be a good idea to use template_redirect
action:
function redirect_if_single_product()
{
if (is_product()) {
wp_safe_redirect(esc_url(home_url()));
exit();
}
}
add_action('template_redirect', 'redirect_if_single_product');
Old question but none of the answers worked for me.
Here is mine adapted from Ale's and MhdSyrwan's ones
add_action('wp','prevent_access_to_product_page');
function prevent_access_to_product_page(){
if ( is_product() ) {
wp_redirect( get_permalink( 269 ) );
}
}
© 2022 - 2024 — McMap. All rights reserved.