John Davidson

php - Transition of Woocommerce Percentage Review Chart to Numeric

0 comments
Message:


On my website's homepage, I use a chart that counts all Woocommerce reviews. These reviews are shown as a percentage for each rating (1 star, 2 star etc ...). I need to change functionality to the total number of ratings for each rating section instead of the percentage and display stars instead of labels.


Any advice?


Now:



Goal:


Code of the graph:


function display_all_product_review_histogram($minimum_rating, $maximum_rating){

$all_product_review_average_rating = get_all_product_review_average_rating($minimum_rating, $maximum_rating);
$total_ratings = $all_product_review_average_rating[0]["total_ratings"];

$get_all_product_review_counts_by_ratings = get_all_product_review_counts_by_ratings($minimum_rating, $maximum_rating);

if($get_all_product_review_counts_by_ratings){
$output = '';
$sum = 0;
$total = 0;
$raw_percentages_array = array();
$percentages_array = array();

//When working with rounded percentages, we must make sure the total percentages add up to 100%.
//Creating array of rating values and its percentage
foreach ($get_all_product_review_counts_by_ratings as $key => $rating) {
$percentage = round($rating["amount"] / $total_ratings, 2) * 100;
$raw_percentages_array[] = array("value" => $rating["value"], "percent_of_total" => $percentage);
}
//Counting the total of our percents
foreach($raw_percentages_array as $key => $percent) {
$total += $percent[ "percent_of_total" ];
}
//Creating an array that will have the actual percentages after the rounding has been applied to it.
//This will help to see if we have 100% or we are not aligned
foreach($raw_percentages_array as $key => $percent){
$percentages_array[$percent["value"]] = round(($percent["percent_of_total"]/$total) * 100, 0);
}
$sum = array_sum($percentages_array); //Again counting the total of our new percents to see if it adds up to 100%

if($sum != 100){ //If we do not have 100%, then we will alter the highest percentage value so that we get a total of 100%
$highest_percentage_key = array_keys($percentages_array, max($percentages_array)); //Getting key of the highest percentage value
$percentages_array[$highest_percentage_key[0]] = 100 - ($sum - max($percentages_array)); //Alterning the percentage
}

//Now we are ready to create the output that will give us 100% in total
$output .= "<div class='product-review-histogram'>";
foreach ($percentages_array as $key => $percentage) {
$output .= "<div class='histogram-row star-rating-". $key ."'>";
$output .= "<div class='histogram-col-1'>". $key ." star</div>";
$output .= "<div class='histogram-col-2'><div class='histogram-meter-bar'><div class='histogram-bar-temperature' style='width: ". $percentage ."%'></div></div></div>";
$output .= "<div class='histogram-col-3'>". $percentage ."%</div>";
$output .= "</div>";
}
$output .= "</div>";

return $output;
}else{
return;
}
}


My Answer:
$total_ratings = get_all_product_review_count($minimum_rating, $maximum_rating); // Get total number of ratings for each rating section
$total_reviews = array_sum($total_ratings); // Get total number of reviews
$rating_labels = array(1, 2, 3, 4, 5); // Define rating labels
$rating_stars = array('★', '★★', '★★★', '★★★★', '★★★★★'); // Define rating stars

// Display the chart
echo '
';
for($i = 0; $i < count($total_ratings); $i++){
$rating = $i + 1;
$rating_count = $total_ratings[$i];
$rating_percentage = ($rating_count / $total_reviews) * 100;
echo '
';
echo '' . $rating_stars[$i] . '';
echo '' . $rating_count . '';
echo '
';
}
echo '
';
}

// Call the function with minimum and maximum rating parameters
display_all_product_review_histogram(1, 5);

Rate this post

3 of 5 based on 8056 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us