I am trying to hide the single product detail page on my wordpress-woocommerce site. How can i achieve this without breaking woocommerce functionality?
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 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.
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.
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 ) );
}
}
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');