对于特定的产品编号你可以使用一些专用的WC_Product
方法:
// Get an instance of the WC_Product Object (from a product ID)
$product = wc_get_product( $product_id);
// The product rating count (number of reviews by rating )
$rating_count = $product->get_rating_counts(); // Multidimensional array
// The product average rating (or how many stars this product has)
$average_rating = $product->get_average_rating();
// Testing Output
echo '<p>Rating average: '.$average_rating.' stars</p>';
显示产品“星星”平均评级:
您可以使用专用功能wc_get_rating_html() https://docs.woocommerce.com/wc-apidocs/source-function-wc_get_rating_html.html#2753-2771 with get_average_rating() https://docs.woocommerce.com/wc-apidocs/source-class-WC_Product.html#669-677 WC_Product
方法。所以必要的代码将是:
// Get an instance of the WC_Product Object (from a product ID)
$product = wc_get_product( $product_id);
// The product average rating (or how many stars this product has)
$average_rating = $product->get_average_rating();
// The product stars average rating html formatted.
$average_rating_html = wc_get_rating_html($average_rating);
// Display stars average rating html
echo $average_rating_html;
经过测试并有效。
一个有趣的答案: 评级显示数字而不是星星 https://stackoverflow.com/questions/49378284/rating-is-showing-numbers-instead-of-stars/49426458#49426458
随处显示产品星级的短代码 - 2 个代码版本:
1)最好的方法基于wc_get_rating_html() https://docs.woocommerce.com/wc-apidocs/source-function-wc_get_rating_html.html#2753-2771功能(适用于 Woocommerce 3+):
add_shortcode( 'product_rating', 'display_the_product_rating' );
function display_the_product_rating( $atts ) {
// Shortcode attributes
$atts = shortcode_atts( array(
'id' => '',
), $atts, 'product_rating' );
if ( isset($atts['id']) && $atts['id'] > 0 ):
// Get an instance of the WC_Product Object
$product = wc_get_product( $atts['id'] );
// The product average rating (or how many stars this product has)
$average = $product->get_average_rating();
endif;
if ( isset($average) ) :
return wc_get_rating_html($average);
endif;
}
2)老方法(也有效):
add_shortcode( 'product_rating', 'display_the_product_rating' );
function display_the_product_rating( $atts ) {
// Shortcode attributes
$atts = shortcode_atts( array(
'id' => '',
), $atts, 'product_rating' );
if ( isset($atts['id']) && $atts['id'] > 0 ):
// Get an instance of the WC_Product Object
$product = wc_get_product( $atts['id'] );
// The product average rating (or how many stars this product has)
$average = $product->get_average_rating();
// HERE the average width
$average_width = $average * 16 . 'px';
endif;
if ( isset($average) ) :
return '<div class="starwrapper" itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
<span class="star-rating" title="'.sprintf(__('Rated %s out of 5', 'woocommerce'), $average).'">
<span style="width:'.$average_width.'">
<span itemprop="ratingValue" class="rating">'.$average.'</span>
</span>
</span>
</div><br clear="all">';
endif;
}
代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。
使用示例:
1) 在 WordPress 页面或帖子文本编辑器中(Where 37
是产品 ID):
[product_rating id='37']
2)在php代码中:
echo do_shortcode( "[product_rating id='37']" );
3)html标签之间:
<?php echo do_shortcode( "[product_rating id='37']" ); ?>
You will get something like: