在 Woocommerce 的某些页面上从产品 ID 添加星级
Add star rating from a product ID on some pages in Woocommerce
有没有办法在自定义页面、自定义位置调用特定产品的星级评分?换句话说,例如,我可以在主页上的 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>';
显示商品"stars"平均评分:
您可以使用专用函数wc_get_rating_html()
with 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']" ); ?>
你会得到这样的东西:
有没有办法在自定义页面、自定义位置调用特定产品的星级评分?换句话说,例如,我可以在主页上的 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>';
显示商品"stars"平均评分:
您可以使用专用函数wc_get_rating_html()
with 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']" ); ?>
你会得到这样的东西: