How to disable/hide woocommerce single product page?
Asked Answered
P

7

16

I am trying to hide the single product detail page on my wordpress-woocommerce site. How can i achieve this without breaking woocommerce functionality?

Path answered 26/5, 2017 at 9:7 Comment(0)
R
15

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
);
Richthofen answered 26/5, 2017 at 9:25 Comment(0)
E
13

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;
}
Echidna answered 5/7, 2017 at 14:31 Comment(1)
This is not only hiding the product page, but also setting all the products as invisible. The correct answer is @Ibad Shah'sStraightjacket
S
7

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.

Synaesthesia answered 10/5, 2018 at 7:11 Comment(0)
C
2

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.

Coercion answered 26/5, 2017 at 9:24 Comment(0)
O
1

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.

Oligopsony answered 25/3, 2021 at 12:6 Comment(0)
S
1

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');
Sedimentary answered 18/9, 2022 at 15:37 Comment(0)
A
0

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 ) );
  }
}
Archegonium answered 8/6, 2021 at 9:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.