WooCommerce: Change priority of tabs by number of product ratings

I have this code that set reviews tab to show first before item description.

add_filter( 'woocommerce_product_tabs', 'woo_reorder_tabs', 98 );
function woo_reorder_tabs( $tabs ) {
    $tabs['reviews']['priority'] = 5;           // Reviews first
    $tabs['description']['priority'] = 10;      // Description second
    return $tabs;
}

Its works fine, But i want to set it to do it only if the number of reviews high from 0.
Something like :

Read More
function woo_reorder_tabs( $tabs ) {
    if(is_review()){
        $tabs['reviews']['priority'] = 5;           // Reviews first
        $tabs['description']['priority'] = 10;      // Description second
        return $tabs;
    }
}

Is there any hook/ function / filter to get number of product reviews?

Thanks.

Related posts

Leave a Reply

2 comments

  1. reviews in woocommerce is just comments in wordpress… so using get_comments_number should work.

    function woo_reorder_tabs( $tabs ) {
        if(get_comments_number() > 0){
            $tabs['reviews']['priority'] = 5;           // Reviews first
            $tabs['description']['priority'] = 10;      // Description second
        }
        return $tabs;
    }
    
  2. Code didn’t work above. You need to add in both parts for it to work. See below.

    /*Reorder Reviews tab to be first*/
    add_filter( 'woocommerce_product_tabs', 'woo_reorder_tabs', 98 );
    function woo_reorder_tabs( $tabs ) {
        if(get_comments_number() > 0){
        $tabs['reviews']['priority'] = 5;           // Reviews first
        $tabs['description']['priority'] = 10;      // Description second
        }
            return $tabs;
    }