Skip to content
Advertisement

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 :

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.

Advertisement

Answer

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;
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement