Woocommerce - 根据所选产品类别重命名产品选项卡

Woocommerce - rename product tab according to the product category selected

我希望根据所选的产品类别将单个产品页面上的默认产品描述标签重命名为不同的名称。

我最初是从以下 WC 函数中重命名默认描述选项卡的:

add_filter( 'woocommerce_product_tabs', 'woo_rename_tabs', 98 );
function woo_rename_tabs( $tabs ) {

    $tabs['description']['title'] = __( 'More Information' );   // Rename the description tab

    return $tabs;

} 

现在我想要实现的是,如果产品被分配到类别 "Books",那么描述选项卡的名称将更改为 "About this book"。 然后,如果产品被分配到类别 "Videos",则选项卡将更改为 "About this video",如果产品被分配到任何其他类别,选项卡名称将更改为 "More Information"

我尝试修改代码,结果是这样的:

add_filter( 'woocommerce_product_tabs', 'woo_rename_tabs', 98 );
function woo_rename_tabs( $tabs ) {

    global $product, $woocommerce;

    if ( is_product_category() ) {

        if ( is_product_category( 'books' ) ) {
           $tabs['description']['title'] = __( 'About this book' );
        } elseif ( is_product_category( 'videos' ) ) {
           $tabs['description']['title'] = __( 'About this video' );  
        } else {
           $tabs['description']['title'] = __( 'More Information' );
        }
    }

    return $tabs;
}

但不幸的是它似乎没有用,因为选项卡名称仍然是 "Description"

我做错了什么?

变化:

 is_product_category( 'books' ) to has_term( 'books', 'product_cat' )
 is_product_category( 'videos' ) to has_term( 'videos', 'product_cat' )

,它应该可以工作。

函数 is_product_category returns 仅当查看产品类别页面时为真。但是您在单个产品页面上,这就是它 returns FALSE 的原因。