douhu2370 2017-08-22 12:04
浏览 78
已采纳

WooCommerce - 如果产品没有类别,则隐藏相关产品

I am wondering if there is a way to hide the related product tab in the single product page only if the product has no categories set.

This is what I tried:

global $product; 

// Return the product categories
$categories = $product->get_categories( ', ', ' ' . _n( ' ', '  ', $cat_count, 'woocommerce' ) . ' ', ' ' );

if(empty($categories)){
        remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
    }

This was added to functions.php but it is not working. Anyone would have a better idea?

  • 写回答

1条回答 默认 最新

  • doudong8713 2017-08-22 12:23
    关注

    Yes it is possible, Try below code.

    function remove_related_product()
    {
        global $product; 
    
        $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
        $terms = wp_get_post_terms( $product_id, 'product_cat' );
    
        if(empty($terms))
        {
             remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
        }
    }
    add_action('woocommerce_after_single_product_summary', 'remove_related_product',10);
    

    Code goes in function.php file of your active child theme (or theme).

    This code is tested on WooCommerce 3+ and works

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

报告相同问题?

悬赏问题

  • ¥15 (标签-matlab)
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错
  • ¥15 java python或者任何一种编程语言复刻一个网页
  • ¥20 如何通过代码传输视频到亚马逊平台
  • ¥15 php查询mysql数据库并显示至下拉列表中
  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信