I’ve succeeding in removing the (0) on the Reviews tab heading when there is no reviews. In marketing – it’s probably best practice to not show that a product has 0 reviews. Here is the code that I have placed in my child theme’s functions.php file which is found in the WooCommerce plugin file wc-template-function.php:
if ( ! function_exists( 'woocommerce_default_product_tabs' ) ) { /** * Add default product tabs to product pages. * * @param array $tabs * @return array */ function woocommerce_default_product_tabs( $tabs = array() ) { global $product, $post; // Description tab - shows product content if ( $post->post_content ) { $tabs['description'] = array( 'title' => __( 'Description', 'woocommerce' ), 'priority' => 10, 'callback' => 'woocommerce_product_description_tab' ); } // Additional information tab - shows attributes if ( $product && ( $product->has_attributes() || ( $product->enable_dimensions_display() && ( $product->has_dimensions() || $product->has_weight() ) ) ) ) { $tabs['additional_information'] = array( 'title' => __( 'Additional Information', 'woocommerce' ), 'priority' => 20, 'callback' => 'woocommerce_product_additional_information_tab' ); } // Reviews tab - shows comments if ( comments_open() ) { $check_product_review_count = $product->get_review_count(); if ( $check_product_review_count == 0 ) { $tabs['reviews'] = array( 'title' => sprintf( __( 'Reviews', 'woocommerce' ) ), 'priority' => 30, 'callback' => 'comments_template' ); } else { $tabs['reviews'] = array( 'title' => sprintf( __( 'Reviews (%d)', 'woocommerce', $product->get_review_count() ), $product->get_review_count() ), 'priority' => 30, 'callback' => 'comments_template' ); } } return $tabs; } }
My question is – is this the most efficient way of modifying this without changing the core files of woocommerce? The function “woocommerce_default_product_tabs” is a pluggable function, but it seems like I could somehow use a filter instead of copying this entire function into my child theme and editing it from there. I just need to get at this line of code:
title' => sprintf( __( 'Reviews (%d)', 'woocommerce', $product->get_review_count() ),
And add an if statement to check if there is no comments to change this above line like in the line above:
title' => sprintf( __( 'Reviews', 'woocommerce' ),
Advertisement
Answer
It’s pretty easy. You can change the title of any tabs:
add_filter( 'woocommerce_product_tabs', 'wp_woo_rename_reviews_tab', 98); function wp_woo_rename_reviews_tab($tabs) { global $product; $check_product_review_count = $product->get_review_count(); if ( $check_product_review_count == 0 ) { $tabs['reviews']['title'] = 'Reviews'; } else { $tabs['reviews']['title'] = 'Reviews('.$check_product_review_count.')'; } return $tabs; }