【问题标题】:Product review doesn't show woocommerce产品评论未显示 woocommerce
【发布时间】:2021-05-06 09:11:57
【问题描述】:

您好,我在 woocommerce 上启用了评论,但未显示在产品页面上,我将此代码放在 sn-ps 中

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 中的启用评论

【问题讨论】:

  • 你把这个woocommerce_after_single_product短代码放在哪里了?
  • 在functions.php中
  • 你把[woocommerce_after_single_product]放在哪里了?
  • 这是我使用的简码 [product_reviews id="id for product"] 而不是 [woocommerce_after_single_product]。我把它放在产品描述中
  • 你应该输入这个[woocommerce_after_single_product]而不是[product_reviews ]短代码

标签: wordpress woocommerce


【解决方案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;

}

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

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' );

经过测试并且有效。

【讨论】:

  • 你能帮我把代码放在functions.php中吗?
  • 你可以在functions.php文件的任何地方添加它。
  • 我使用了 cmets_template() 并且它出现在 functions.php 中
猜你喜欢
  • 2012-06-30
  • 2019-06-02
  • 2021-05-05
  • 1970-01-01
  • 2013-01-31
  • 2016-11-29
  • 2017-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多