如何解决以简码显示WooCommerce评论
我正在使用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;
}
立即出现问题 现在,我想编辑简码,以便可以拉出 /还显示平均总产品评分(可能还有一个表格,但为此,我只想访问其他评论提交页面)。
解决方法
要计算总平均值,您必须计算评论总数并计算评分总数。然后划分评分/评论。检查我下面的代码
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;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。