有没有办法在自定义页面、自定义位置调用特定产品的星级评级?换句话说,我是否可以添加特定 T 恤的星级,以显示在主页上该 T 恤的图片下方?
我想我需要向编辑器添加某种 php,然后通过某种 html 调用它。我看到this answered thread ,其中一个答案似乎对人们有帮助,但是,它只为您提供了必要的 php,所以我不知道如何实际将其放在特定页面上的特定位置。我也不确定这个答案是否解决了在自定义页面上添加星星的可能性,而不仅仅是 woocommerce 页面。
该线程的另一个答案包含 html。我尝试了一下,我能够加载星星,只是它们与产品页面上的实际星级评级不符。
对于类似的问题已经存在这一事实,我深表歉意,但是,它们似乎没有直接满足我的需求,而且我太无能,无法使它们发挥作用:/。
最佳答案
对于特定的产品 ID,您可以使用一些专用的 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()
与 get_average_rating()
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;
经过测试并有效。
An interesting answer: Rating is showing numbers instead of stars
用于在各处显示产品星级的短代码 - 2 个代码版本:
1)基于wc_get_rating_html()
的最佳方法函数(适用于 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 页面或帖子文本编辑器中(其中 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']" ); ?>
关于php - 在 Woocommerce 的某些页面上添加产品 ID 的星级评级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52123797/