【问题标题】:WooCommerce: Remove reviews if they are emptyWooCommerce:如果评论为空,则删除评论
【发布时间】:2020-07-12 00:25:44
【问题描述】:

如果没有公开评论,我想删除评论部分。 我找到了删除评论标签的解决方案。但我已经将评论移到了另一个位置。

这是我移动评论内容的代码(我也禁用了标签):

// display tab content elsewhere
function woocommerce_template_product_reviews() {
    woocommerce_get_template( 'single-product-reviews.php' );
}
add_action( 'woocommerce_after_single_product_summary', 'comments_template', 30 );

// remove the tab
add_filter( 'woocommerce_product_tabs', 'remove_product_tabs', 98 );
function remove_product_tabs( $tabs ) {
    unset( $tabs['reviews'] );
    return $tabs;
}

如果没有评论,这是我发现的删除标签的代码:

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

    global $product;
    $id = $product->id;

    $args = array ('post_type' => 'product', 'post_id' => $id);    
    $comments = get_comments( $args );

    if(empty($comments)) {
        unset( $tabs['reviews'] );
    }

    return $tabs;
}

在这里找到它:https://stackoverflow.com/a/33433478/1788961

问题是,此代码仅在评论仍在选项卡中时才有效。

我尝试了以下代码,但不起作用:

function woocommerce_template_product_reviews() {

    global $product;
    $id = $product->id;

    $args = array ('post_type' => 'product', 'post_id' => $id);
    $comments = get_comments( $args );

    if(!empty($comments)) {
        woocommerce_get_template( 'single-product-reviews.php' );
    }


}
add_action( 'woocommerce_after_single_product_summary', 'comments_template', 30 );

如果没有公开评论,还有其他方法可以隐藏评论吗?

【问题讨论】:

  • “这是我移动评论内容的代码”——此时所做的只是加载一个特定的模板。解释它如何在任何地方“移动”任何东西。如果您在某些默认模板的副本中进行了更改(?),然后显示这些是什么。
  • 模板将评论加载到主题中的另一个位置。我将删除标签中评论的代码添加到我的问题中

标签: php wordpress woocommerce


【解决方案1】:

您只需在移动审核模板之前进行相同的验证。

因此,如果有此代码的注释,您只会显示您的模板:

function woocommerce_template_product_reviews() {
    global $product;
    $id = $product->get_id();

    $args = array ('post_type' => 'product', 'post_id' => $id);    
    $comments = get_comments( $args );

    if(!empty($comments)) {
    wc_get_template( 'single-product-reviews.php' );
    }
}
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_product_reviews', 30 );

旁注,我还将$product-> id(); 更改为已弃用的新版本:$product->get_id();

因此,您还需要编辑代码以删除产品上的评论标签而不加评论:

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

    global $product;
    $id = $product->get_id();

    $args = array ('post_type' => 'product', 'post_id' => $id);    
    $comments = get_comments( $args );

    if(empty($comments)) {
        unset( $tabs['reviews'] );
    }

    return $tabs;
}

【讨论】:

    猜你喜欢
    • 2016-01-30
    • 1970-01-01
    • 2011-12-18
    • 2012-03-09
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    相关资源
    最近更新 更多