WC_Comments::get_average_rating_for_product() public WC 3.0.0
Get product rating for a product. Please note this is not cached.
{} Это метод класса: WC_Comments{}
Хуков нет.
Возвращает
float.
Использование
$result = WC_Comments::get_average_rating_for_product( $product );
- $product(WC_Product) (обязательный) (передается по ссылке — &)
- Product instance.
Список изменений
С версии 3.0.0 | Введена. |
Код WC_Comments::get_average_rating_for_product() WC Comments::get average rating for product WC 5.0.0
public static function get_average_rating_for_product( &$product ) {
global $wpdb;
$count = $product->get_rating_count();
if ( $count ) {
$ratings = $wpdb->get_var(
$wpdb->prepare(
"
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 = %d
AND comment_approved = '1'
AND meta_value > 0
",
$product->get_id()
)
);
$average = number_format( $ratings / $count, 2, '.', '' );
} else {
$average = 0;
}
return $average;
}