Search code examples
phpwordpresswoocommerceproductrating

Remove review stars if no reviews in WooCommerce shop and archives


I want to not display the stars on products in the Shop page loop. My current code doesn't work...

I'm working on a WooCommerce website with Astra.

function no_stars_if_no_reviews() {
    global $product;
    if ($average = $product->get_average_rating()) :
if($average > 0) {
remove_filter(get_average_rating);
}
}

I just want to remove the filter when no reviews are there, but keep it otherwise. Once again, this is for the Products page, NOT single.


Solution

  • Simply use the following, to remove rating stars from shop and archive pages when no reviews:

    add_action( 'woocommerce_after_shop_loop_item_title', 'conditionally_remove_loop_rating', 4 );
    function conditionally_remove_loop_rating(){
        global $product;
    
        if( ! ( $product->get_review_count() > 0 ) ) {
            remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
        }
    }
    

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