Answer the question
In order to leave comments, you need to log in
How to make product rating on OpenCart 2.3 like on Wildberries?
Hello. Looked for a plugin but couldn't find it. How can I display the product rating on the product page in this way?
Now only the following variables are sent to the template as standard: Total rating
rounded to the nearest integer: $rating
Number of ratings: $rating_data
How can I get the number of ratings with a rating of 1, with a rating of 2, etc.?
Answer the question
In order to leave comments, you need to log in
I was able to make this (OpenCart 3.0.3.2):
In the controller catalog/controller/product/product.php
After the line Insert
$data['rating'] = (int)$product_info['rating'];
// Expanded rating
$this->load->model('catalog/review');
$review_total = $this->model_catalog_review->getTotalReviewsByProductId($this->request->get['product_id']);
$estimations = $this->model_catalog_review->getReviewsByProductId($this->request->get['product_id']);
$one = 0;
$two = 0;
$three = 0;
$four = 0;
$five = 0;
$rating_sum = 0;
foreach ($estimations as $estimation){
($estimation['rating'] == '1') && $one++;
($estimation['rating'] == '2') && $two++;
($estimation['rating'] == '3') && $three++;
($estimation['rating'] == '4') && $four++;
($estimation['rating'] == '5') && $five++;
$rating_sum += $estimation['rating'];
}
$data['expanded_rating'][] = array(
'one' => round((100/$review_total) * $one),
'two' => round((100/$review_total) * $two),
'three' => round((100/$review_total) * $three),
'four' => round((100/$review_total) * $four),
'five' => round((100/$review_total) * $five),
'rating' => round($rating_sum / $review_total, 1),
'total' => $review_total
);
catalog/view/theme/default/template/product/product.twig
<p>{% for i in 1..5 %}
{% if rating < i %}<span class="fa fa-stack"><i class="fa fa-star-o fa-stack-1x"></i></span>{% else %}<span class="fa fa-stack"><i class="fa fa-star fa-stack-1x"></i><i class="fa fa-star-o fa-stack-1x"></i></span>{% endif %}
{% endfor %} <a href="" onclick="$('a[href=\'#tab-review\']').trigger('click'); return false;">{{ reviews }}</a> / <a href="" onclick="$('a[href=\'#tab-review\']').trigger('click'); return false;">{{ text_write }}</a></p>
<hr>
{% for rating in expanded_rating %}
<p>1 - {{ rating.one }}%</p>
<p>2 - {{ rating.two }}%</p>
<p>3 - {{ rating.three }}%</p>
<p>4 - {{ rating.four }}%</p>
<p>5 - {{ rating.five }}%</p>
<h3>Общий рейтинг - {{ rating.rating }}</h3>
<p>На основании {{ rating.total }} отзывов</p>
{% endfor %}
<hr>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question