你如何在woocommerce中为产品添加星级? [英] How do you add the star ratings for products in woocommerce?

查看:24
本文介绍了你如何在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_titlewoocommerce_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆