我正在制作产品评分,我只想添加评分而不是评论。 这是我的代码。 内容单product.php
<div class="description">
<?php
/**
* woocommerce_single_product_summary hook
*
* @hooked woocommerce_template_single_title - 5
* @hooked woocommerce_template_single_rating - 10
* @hooked woocommerce_template_single_price - 10
* @hooked woocommerce_template_single_excerpt - 20
* @hooked woocommerce_template_single_add_to_cart - 30
* @hooked woocommerce_template_single_meta - 40
* @hooked woocommerce_template_single_sharing - 50
*/
do_action( 'woocommerce_single_product_summary' );
?>
</div>
的functions.php
add_action('woocommerce_after_single_product_summary', 'my_print_stars' );
function my_print_stars(){
global $wpdb;
global $post;
$count = $wpdb->get_var("
SELECT COUNT(meta_value) FROM $wpdb->commentmeta
LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
WHERE meta_key = 'rating'
AND comment_post_ID = $post->ID
AND comment_approved = '1'
AND meta_value > 0
");
$rating = $wpdb->get_var("
SELECT SUM(meta_value) FROM $wpdb->commentmeta
LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
WHERE meta_key = 'rating'
AND comment_post_ID = $post->ID
AND comment_approved = '1'
");
if ( $count > 0 ) {
$average = number_format($rating / $count, 2);
echo '<div class="starwrapper" itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">';
echo '<span class="star-rating" title="'.sprintf(__('Rated %s out of 5', 'woocommerce'), $average).'"><span style="width:'.($average*16).'px"><span itemprop="ratingValue" class="rating">'.$average.'</span> </span></span>';
echo '</div>';
}
}
主题/新/ INC / woocommerce / hooks.php remove_action(
remove_action( 'woocommerce_after_single_product_summary','woocommerce_template_single_rating',10 );
add_action( 'woocommerce_after_single_product_summary','woocommerce_template_single_rating',10 );
这是代码。问题是评级部分未显示在产品详细信息页面中。这段代码有什么问题吗?或者我必须添加新的。