您如何添加woocommerce产品的星级? [英] How do you add the star ratings for products in woocommerce?
问题描述
我有一个woocommerce商店,当您看到它们的缩略图时,我想为每个产品添加星级。我已经在大产品视图中看到了星星,但是我希望它们像大多数电子商务商店(例如timberland.com)一样,在每个缩略图下方显示。我知道我可以使用CSS从视图中禁用项目,但不能添加它们。有什么想法吗?
I have a woocommerce store and I want to add star ratings to each of the products when you see their thumbnails. I already have the stars in the big product view but I want them to display below each thumbnail like in most ecommerce stores like timberland.com. I know i can use css to disable items from view but not add them. Any thoughts?
推荐答案
您可以将其放入主题functions.php文件中:
You can put this into your themes functions.php file:
add_action('woocommerce_after_shop_loop_item', '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>';
}
}
请注意,您可能需要更改 woocommerce_after_shop_loop_item
取决于您的设计以及您希望星星显示在何处的钩子。
Note that you may need to change woocommerce_after_shop_loop_item
to a different hook depending on your design and where exactly you want the stars to show up.
此页面列出 WooCommerce 操作挂钩: http://wcdocs.woothemes.com/codex/extending/hooks/ 。我没有看到与该缩略图相关的任何挂钩,但是您可以尝试 woocommerce_after_shop_loop_item_title
或 woocommerce_after_shop_loop_item
。
This page lists WooCommerce action hooks: http://wcdocs.woothemes.com/codex/extending/hooks/ . I don't see any hooks associated with the thumbnail specifically, but you may try woocommerce_after_shop_loop_item_title
or woocommerce_after_shop_loop_item
.
(此功能实际上是从WooCommerce的single-product-reviews.php复制的)
(This function is essentially copied from WooCommerce's single-product-reviews.php)
这篇关于您如何添加woocommerce产品的星级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!