Magento reviews are showing summary-rating instead of rating-items in php
In my Magento store customers are reviewing my products in 3 categories (i.e. price). But when I'm printing these ratings it only shows a summary instead of these 3 categeries(see code).
<?php
//from http://www.exploremagento.com/magento/run-magento-code-outside-of-magento开发者_如何学编程.php
require_once '../app/Mage.php';
umask(0);
Mage::app('default');
$review = Mage::getModel('review/review');
$collection = $review->getProductCollection();
$collection
->addAttributeToSelect('*')
->getSelect()
->limit(5)
->order('rand()');
$review->appendSummary($collection);
foreach($collection as $product) {
//var_dump($product->debug());
}
/* To get (what I assume is) 'star' rating. */
$ratingSummary = $product->getRatingSummary();
$starRating = $ratingSummary['rating_summary'];
echo $starRating . "<br/>";
?>
How can I get all the ratings instead of the summary?
I've created this function in helper object, just to display simple html for average product rating (also 3 categories). Hope you find it helpfull.
public function getRatingHtml($product_id) {
$store_id = Mage::app()->getStore()->getId();
$query = "
SELECT
ROUND(AVG(`percent_approved`)) as `rating`
FROM
`rating_option_vote_aggregated`
WHERE
`entity_pk_value` = {$product_id}
AND
`store_id` = {$store_id}";
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$rating = $read->query($query)->fetch();
$html = "
<a href=\"/review/product/list/id/{$product_id}/\" class=\"rating-box-link\">
<span class=\"rating-box\">
<span class=\"rating\" style=\"width: {$rating['rating']}%;\"></span>
</span>
</a>";
return $html;
}
精彩评论