【问题标题】:How to display Woo Commerce reviews using a shortcode?如何使用简码显示 Woo Commerce 评论?
【发布时间】:2020-08-27 15:41:50
【问题描述】:

我正在使用 WooCommerce 的 WYSIWYG 工具构建自定义产品页面。为了 CRO 的缘故,我想在页面底部添加评论。

我已经禁用了标签(评论、描述),我将在中间添加自定义内容,在底部添加评论。

我已经找到了一种显示评论的方法(来源:https://www.businessbloomer.com/woocommerce-display-product-reviews-custom-page-shortcode/

add_shortcode( 'product_reviews', 'bbloomer_product_reviews_shortcode' );
 
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;
} 

现在有问题 现在我想编辑简码,以便可以拉 /显示总平均产品评分(可能还有一个表格,但为此我可以喜欢不同的评论提交页面)。

【问题讨论】:

  • 请在所见即所得工具中输入[product_reviews]。调用您的简码。请参考此网址wpbeginner.com/wp-tutorials/how-to-add-a-shortcode-in-wordpress
  • 抱歉,我不确定我是否理解您的解决方案。添加短代码将完成我已经在做的事情 - 通过其 ID 显示产品的评论。我想“复制”或接近它,woocommerce 使用简码审查功能。不仅意味着评级(正如 businessbloomer.com 的代码所做的那样),还意味着平均评级等。
  • 你能分享你如何在所见即所得中使用这个短代码。你必须像这样传递产品 id [product_reviews id="your product id"]
  • 是的,这就是我已经使用它的方式,这就是为什么我不明白你的解决方案。我已经以这种方式使用了简码

标签: php wordpress woocommerce e-commerce


【解决方案1】:

要计算总平均数,您必须计算评论总数并计算评分总数。然后划分评级/评论。检查我下面的代码

add_shortcode( 'product_reviews', 'product_reviews_shortcode' );

function product_reviews_shortcode( $atts ) {
    
   if ( empty( $atts ) ) return '';
 
   if ( ! isset( $atts['id'] ) ) return '';
       
   $comments = get_comments( 'post_id=' . $atts['id'] );
   
   $total_comments = count( $comments );
   
   if ( ! $comments ) return '';
   
   $total_rating = 0;

   $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 ) );
      $total_rating = $total_rating + $rating;
      $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>';
    
    if( $total_rating > 0 ) {
       $total_average = $total_rating / $total_comments;
       $total_average =  number_format($total_average, 2, '.', '');
    }

   return $html;
}

【讨论】:

  • 这破坏了现有的 cmets:停止显示它们,只显示放大的头像图像,并且评论丢失了
  • 为我工作我在我的本地主机测试。
  • 感谢您的意见,但它不起作用。我又试了一次。它破坏了现有短代码的输出(上面发布)。我通过代码禁用了 woo 评论选项卡,这可能是原因吗?话虽如此,上面的简码在评论标签被禁用时有效。
  • 你能告诉我到底是什么破坏了吗?可以分享截图吗?
  • 当然,这里有 2 个屏幕截图:我的代码与您的代码,在 google docs docs.google.com/document/d/…
猜你喜欢
  • 1970-01-01
  • 2020-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-05
  • 1970-01-01
  • 2015-07-13
  • 2020-08-29
相关资源
最近更新 更多