mickmickmick
mickmickmick

Reputation: 337

Remove Woocommerce review tab if empty

How can I hide the Review tab for products without reviews only? I've found this code:

add_filter( 'woocommerce_product_tabs', 'delete_tab', 98 ); function delete_tab( $tabs ) { unset($tabs['reviews']); return $tabs; }

But it removes the Reviews everywhere, even in products that do have some reviews.

Upvotes: 3

Views: 8026

Answers (6)

Purnendu Sarkar
Purnendu Sarkar

Reputation: 342

Can you also try it

function ps_disable_reviews() {
        remove_post_type_support( 'product', 'comments' );
    }
    
add_action( 'init', 'ps_disable_reviews' );

Upvotes: 0

Michał Miszczyszyn
Michał Miszczyszyn

Reputation: 12711

The simplest way is to add a filter to woocommerce_product_tabs. Inside it, you can use global $product which is a reference to a current product. That object has a method called get_review_count:

add_filter('woocommerce_product_tabs', function ($tabs) {
    global $product;

    if ($product && $product->get_review_count() === 0) {
        unset($tabs['reviews']);
    }

    return $tabs;
}, 98);

Upvotes: 2

YASHPAL KUMAR
YASHPAL KUMAR

Reputation: 31

Remove Woocommerce Description tab if empty

add_filter( 'woocommerce_product_tabs', 'delete_description_tab', 98 );
function delete_description_tab( $tabs ) {

    global $product;
    $id = $product->id;

    $data = array ('post_type' => 'product', 'post_id' => $id);    
    $description = get_comments( $data );

    if(empty($description)) {
        unset( $tabs['description'] );
    }

    return $tabs;
}

Upvotes: 0

Callum
Callum

Reputation: 574

how about just bumping other 'tabs' to the bottom, on storefront for example?

Upvotes: 0

0blongCode
0blongCode

Reputation: 84

Here's a similar way to Dimitar's option that's a bit shorter:

add_filter( 'woocommerce_product_tabs', 'delete_tab', 98 );

function delete_tab( $tabs ) {

    if ( ! have_comments() ) {
        unset( $tabs['reviews'] );
    }

    return $tabs;
}

Upvotes: 0

Dimitar Stoyanov
Dimitar Stoyanov

Reputation: 349

Check This:

add_filter( 'woocommerce_product_tabs', 'delete_tab', 98 );
function delete_tab( $tabs ) {

    global $product;
    $id = $product->id;

    $args = array ('post_type' => 'product', 'post_id' => $id);    
    $comments = get_comments( $args );

    if(empty($comments)) {
        unset( $tabs['reviews'] );
    }

    return $tabs;
}

Upvotes: 5

Related Questions