du5739 2017-12-09 06:03
浏览 50
已采纳

Woocommerce - 根据所选的产品类别重命名产品标签[重复]

This question already has an answer here:

I was hoping to rename the default product DESCRIPTION TAB on single product page to a different name according to the product category selected.

I was initially coming out of the following WC function to rename the default description tab:

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;

} 

Now what I would like to achieve is that if the product is assigned to category "Books", then the name of the description tab would change to "About this book". Then If the product is assigned to category "Videos", then the tab would change to "About this video" and if the product is assigned to any other category, the tab name would change to "More Information"

I tried to amend the code and this is what I came up with:

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

But unfortunately it doesn't seem to be working as the tab name is still "Description"

What am I doing wrong?

</div>
  • 写回答

1条回答 默认 最新

  • dsdqpdjpq16640651 2017-12-09 09:53
    关注

    Change:

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

    , and it should work.

    Function is_product_category returns true only when viewing a product category page. But you are on single product page, that's why it returns FALSE.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示