产品评论未显示 woocommerce

时间:2021-05-06 09:11:57

标签: wordpress woocommerce

嗨,我在 woocommerce 上启用了评论,但没有显示在产品页面上,我将此代码放在片段中

function bbloomer_product_reviews_shortcode( $atts ) {

if ( empty( $atts ) ) return '';

if ( ! isset( $atts['id'] ) ) return '';
   
$comments = get_comments( 'post_id=' . $atts['id'] );

if ( ! $comments ) return '';

$html .= '<div class="woocommerce-tabs"><div id="reviews"><ol class="commentlist">';

foreach ( $comments as $comment ) {   
  $rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
  $html .= '<li class="review">';
  $html .= get_avatar( $comment, '60' );
  $html .= '<div class="comment-text">';
  if ( $rating ) $html .= wc_get_rating_html( $rating );
  $html .= '<p class="meta"><strong class="woocommerce-review__author">';
  $html .= get_comment_author( $comment );
  $html .= '</strong></p>';
  $html .= '<div class="description">';
  $html .= $comment->comment_content;
  $html .= '</div></div>';
  $html .= '</li>';
}

$html .= '</ol></div></div>';

return $html;
}
add_shortcode( 'woocommerce_after_single_product', 'bbloomer_product_reviews_shortcode' );

我检查了对所有产品的启用审查,并在 woocommerce 中进行了讨论

1 个答案:

答案 0 :(得分:1)

您可以使用操作挂钩 woocommerce_product_tabs 添加新标签。检查下面的代码。代码位于您的活动主题 functions.php 文件中。

add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {

    $tabs['products_review_tab'] = array(
        'title'     => __( 'Reviews', 'woocommerce' ),
        'priority'  => 120,
        'callback'  => 'products_review_tab_content'
    );

    return $tabs;

}

现在您可以在 echo do_shortcode[] 回调中products_review_tab_content

function products_review_tab_content() {
    global $product;
    echo do_shortcode( '[product_reviews_shortcode id="'.$product->id.'"]' );
}

function product_reviews_shortcode( $atts ) {
    
    $atts = shortcode_atts( array(
        'id' => ''
    ), $atts, 'product_reviews_shortcode' );

    $comments = get_comments( array(
        'post_id' => $atts['id'] 
    ) );

    if ( ! $comments ) return '';

    $html .= '<div class="woocommerce-tabs"><div id="reviews"><ol class="commentlist">';

    foreach ( $comments as $comment ) {   
      $rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
      $html .= '<li class="review">';
      $html .= get_avatar( $comment, '60' );
      $html .= '<div class="comment-text">';
      if ( $rating ) $html .= wc_get_rating_html( $rating );
      $html .= '<p class="meta"><strong class="woocommerce-review__author">';
      $html .= get_comment_author( $comment );
      $html .= '</strong></p>';
      $html .= '<div class="description">';
      $html .= $comment->comment_content;
      $html .= '</div></div>';
      $html .= '</li>';
    }

    $html .= '</ol></div></div>';

    return $html;

}
add_shortcode( 'product_reviews_shortcode', 'product_reviews_shortcode' );

经过测试并有效。

enter image description here