WooCommerce:按产品评分数量更改选项卡的优先级

WooCommerce: Change priority of tabs by number of product ratings

我有这段代码可以将评论选项卡设置为在项目描述之前首先显示。

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;
}

它工作正常,但我想将它设置为仅当评论数从 0 开始时才执行此操作。 像 :

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

是否有任何钩子/函数/过滤器来获取产品评论的数量?

谢谢。

woocommerce 中的评论只是 wordpress 中的评论...所以使用 get_comments_number 应该有效。

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

以上代码无效。您需要添加这两个部分才能正常工作。 见下文。

/*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;
}