在WooCommerce中显示当前用户ID的总评分

时间:2018-06-21 07:19:06

标签: php sql woocommerce product rating

我正在使用以下代码:Display total customers reviews and ratings average in WooCommerce

如何通过此功能获得每个当前用户ID的总产品评分?

类似于get_total_reviews_count()函数,下面的函数来自当前用户ID。

function get_total_reviews_count(){
$user_id = get_current_user_id();

return get_comments(array(
'status' => 'approve',
'post_status' => 'publish',
'post_type' => 'product',
'post_author' => $user_id,
'count' => true
));
}

产品评分功能:

function get_products_ratings(){
global $wpdb;

return $wpdb->get_results("
SELECT t.slug, tt.count
FROM {$wpdb->prefix}terms as t
JOIN {$wpdb->prefix}term_taxonomy as tt ON tt.term_id = t.term_id
WHERE t.slug LIKE 'rated-%' AND tt.taxonomy LIKE 'product_visibility'
ORDER BY t.slug
");
}

function products_rating_average_html(){
$stars = 1;
$average = 0;
$total_count = 0;
foreach( get_products_ratings() as $values ){
$average += $stars * $values->count;
$total_count += $values->count;
$stars++;
}
return '<p class="rating-average">'.round($average / $total_count, 1).' / 5 '. __('Stars average').'</p>';
}

0 个答案:

没有答案