Reputation: 5483
I want to remove the review section if there is no public review. I found a solution to remove the review tab. But I already moved the reviews to another location.
Here's my code which moves the review content (I also disabled the tab):
// display tab content elsewhere
function woocommerce_template_product_reviews() {
woocommerce_get_template( 'single-product-reviews.php' );
}
add_action( 'woocommerce_after_single_product_summary', 'comments_template', 30 );
// remove the tab
add_filter( 'woocommerce_product_tabs', 'remove_product_tabs', 98 );
function remove_product_tabs( $tabs ) {
unset( $tabs['reviews'] );
return $tabs;
}
And here's the code I found to remove the tab if there are no reviews:
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;
}
Found it here: https://stackoverflow.com/a/33433478/1788961
The problem is, that this code only works if the reviews are still in the tab.
I tried the following code but that doesn't work:
function woocommerce_template_product_reviews() {
global $product;
$id = $product->id;
$args = array ('post_type' => 'product', 'post_id' => $id);
$comments = get_comments( $args );
if(!empty($comments)) {
woocommerce_get_template( 'single-product-reviews.php' );
}
}
add_action( 'woocommerce_after_single_product_summary', 'comments_template', 30 );
Is there another way to hide the rewiews if there is no public review?
Upvotes: 1
Views: 1344
Reputation: 1485
You only have to make the same verification before to move your review template.
So you will only show your template if there is a comment with this code:
function woocommerce_template_product_reviews() {
global $product;
$id = $product->get_id();
$args = array ('post_type' => 'product', 'post_id' => $id);
$comments = get_comments( $args );
if(!empty($comments)) {
wc_get_template( 'single-product-reviews.php' );
}
}
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_product_reviews', 30 );
Side note, I also changed $product-> id();
which is deprecated to the new version: $product->get_id();
So you will also need to edit your code to remove review tab on product without comment:
add_filter( 'woocommerce_product_tabs', 'delete_tab', 98 );
function delete_tab( $tabs ) {
global $product;
$id = $product->get_id();
$args = array ('post_type' => 'product', 'post_id' => $id);
$comments = get_comments( $args );
if(empty($comments)) {
unset( $tabs['reviews'] );
}
return $tabs;
}
Upvotes: 2