产品评论功能是Magento众多优秀特征之一。用好评论可以增加客户对你的品牌的信任,由此能显著增加你产品的销量。

有时,你需要在评论页的其他页面展示评价星级。这正是我将要在这篇文章中写到的。

下面这段代码适用于已经读取的产品(或者你知道产品的ID)——比如在购物车中调用产品评价星级;

<?php
$_product = $_item->getProduct(); //get the product in cart
$storeId = Mage::app()->getStore()->getId();
$summaryData = Mage::getModel('review/review_summary')
					->setStoreId($storeId)
					->load($_product->getId());
 
if ($summaryData['rating_summary']):?>
	<div class="ratings">
		<div class="rating-box">
			<div class="rating" style="width:<?php echo $summaryData['rating_summary'] ?>%"></div> 
</div>
</div>
<?php endif; ?>

如果我们访问购物车页面,我们会看到客户评价过的产品都会展示评价星级。

在购物车中显示产品评价星级

在$summaryData对象中,我们可以获取到这个数组的键值如下:

//Entity id of a summary review
["primary_id"] => string(3) "100"
//
//Product id
["entity_pk_value"] => string(3) "119"
//
//Entity type id: 1-Product; 2-Customer; 3-Category
["entity_type"] => string(1) "1"
//
//Qty of reviews
["reviews_count"] => string(1) "2"
//
//Summarized rating: the percentage which represents number of stars, each 20% step fills up one star
["rating_summary"] => string(2) "80"
//
//Store id
["store_id"] => string(1) "1"

记住展示评价星级的“Magento方式”是:创建一个block或者一个有接收product ID和store ID方法的helper,然后从视图文件中返回值。

然而,你也可以将这段代码放入视图文件中来快速地为你的产品获取星级。

注意,要在以上Html 标记(7-11行)之后输出星级的值,不然的话,星级将不会显示。