1. 根据产品id获取该产品评论
$productId = 1234;
$product = Mage::getModel('catalog/product')->load($productId);
$storeId = Mage::app()->getStore()->getId();
Mage::getModel('review/review')->getEntitySummary($product, $storeId);
$ratingSummary = $product->getRatingSummary();
print_r($ratingSummary->getData());
结果是:
Array
(
[primary_id] => 27
[entity_pk_value] => 16
[entity_type] => 1
[reviews_count] => 3
[rating_summary] => 51
[store_id] => 1
)
结果解释如下:
//Entity id of a summary review
["primary_id"] => string(3) "27"
//
//Product id
["entity_pk_value"] => string(3) "16"
//
//Entity type id: 1-Product; 2-Customer; 3-Category
["entity_type"] => string(1) "1"
//
//Qty of reviews
["reviews_count"] => string(1) "3"
//
//Summarized rating: the percentage which represents number of stars, each 20% step fills up one star
["rating_summary"] => string(2) "51"
//
//Store id
["store_id"] => string(1) "1"
或者简单点:
<?php
$storeId = Mage::app()->getStore()->getId();
$summaryData = Mage::getModel('review/review_summary')->setStoreId($storeId)->load($productId);
if($summaryData->getRatingSummary()){
?>
<div class="rating-box" style="float:left;">
<div class="rating" style="width: <?php echo $summaryData->getRatingSummary().'%'; ?>"></div>
</div>
<?php } ?>
2. 获取所有评论
function getReviews() {
$reviews = Mage::getModel('review/review')->getResourceCollection();
$reviews->addStoreFilter( Mage::app()->getStore()->getId() )
->addStatusFilter( Mage_Review_Model_Review::STATUS_APPROVED )
->setDateOrder()
->addRateVotes()
->load();
return $reviews;
}
结合上面的函数,我自己使用的例子如下:
$home_reviews = array();
$storeId = Mage::app()->getStore()->getId();
$reviews = Mage::getModel('review/review')
->getCollection()
->addStoreFilter($storeId)
->addStatusFilter(Mage_Review_Model_Review::STATUS_APPROVED)
->setPageSize(5)
->setCurPage(1)
->setDateOrder();
foreach($reviews as $key=>$review)
{
$home_reviews[$key] = $review->getData();
$home_reviews[$key]['rating'] = Mage::getModel('review/review_summary')->setStoreId($storeId)->load($review->getEntityPkValue())->getRatingSummary();
$home_reviews[$key]['rate'] = number_format($home_reviews[$key]['rating']*5/100,1);
}
这个例子结合了几个条件,1. 状态是 APPROVED 2. 时间排序,最新在前 3. 只获取前五个