1
votes

How would we go about disabling the single product page for specific products?

For example, we have some products that have product variations. In this case we are using the single product page. But for the products that do not have variations we simply use an add to cart link on the landing pages and skip the single product page that just adds an extra step for the customer.

I found this post which outlines how to disable ALL single product pages. But I would like to target the pages that are disabled. Either by product number or perhaps product listing type ie. variable, non variable.

What is the best way to go about doing this, without breaking WooCommerce or causing SEO issues?

To clarify: by disabled I mean remove the link to the page from areas like the shopping cart etc.

2

2 Answers

2
votes

In the following functions, you will have to define one or many product IDs inside the code.

This first hooked function will remove the product from product catalog:

add_filter( 'woocommerce_product_is_visible', 'filter_product_is_visible', 20, 2 );
function filter_product_is_visible( $is_visible, $product_id ){
    // HERE define your products IDs (or variation IDs) to be set as not visible in the array
    $targeted_ids = array(37, 43, 51);

    if( in_array( $product_id, $targeted_ids ) )
        $is_visible = false;

    return $is_visible;
}

To remove the link from cart items in cart page, you can use the following

add_filter( 'woocommerce_cart_item_name', 'filter_cart_item_name', 20, 3 );
function filter_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
    // HERE define your products IDs (or variation IDs) to be set as not visible in the array
    $targeted_ids = array(37, 43, 51);

    if( in_array( $cart_item['data']->get_id(), $targeted_ids ) && is_cart() )
        return $cart_item['data']->get_name();

    return $product_name;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Is also possible to redirect the target products pages to the main shop.

0
votes

You can add if statement around return false. You can either check for product (page) id or I would add tag (or category or custom field) to these and then in if statement you can check for this tag and if its there you return false;