I have a product rating system in place and I calculate average rating on approved ratings. If a product page doesn't have a rating or doesn't have an approved rating I get a White Screen of Death unless I remove the $AverageRating tag from the template.
Below is the Code
function AverageRating() {
$records = DataObject::get("Review","Approval = 1 AND ProductID =" . $this->ID);
$ratingArray = $records->column('Rating');
$totalRatingsCount = $records->Count();
$totalRatingSum = array_sum($ratingArray);
$averageRating = $totalRatingSum/$totalRatingsCount;
if ($averageRating >= ($half = ($ceil = ceil($averageRating))- 0.5) + 0.25) return $ceil;
else if($averageRating < $half - 0.25) return floor($averageRating);
else return $half;
}
I've tried wrapping the rating calculation in in a if (isset($records)) {...}else{return 0;} but that doesn't help