Reputation: 337
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
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
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
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
Reputation: 574
how about just bumping other 'tabs' to the bottom, on storefront for example?
Upvotes: 0
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
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