首页 > 其他分享 >Magento: 获取产品评论 get all reviews with review summary

Magento: 获取产品评论 get all reviews with review summary

时间:2023-03-23 12:02:16浏览次数:48  
标签:string get review getModel summary reviews id Mage


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. 只获取前五个

 


标签:string,get,review,getModel,summary,reviews,id,Mage
From: https://blog.51cto.com/u_8895844/6144617

相关文章

  • SVN - Get remote repository URL
    $svninfofoo.cPath:foo.cName:foo.cURL:http://svn.red-bean.com/repos/test/foo.cRepositoryRoot:http://svn.red-bean.com/repos/testRepository......
  • Fetch 基本操作 Get Post Delete Put
    //删除请求asyncfunctionDeleteModel(model:Customer){leturl=`http://localhost:57679/api/Customers/${model.id}`awaitfetch(url,{method:'dele......
  • Qt QtWidget使用Material风格的组件库
    一、qt-material-widget组件库介绍该组件库拥有炫酷的Material风格的组件,并且该组件库基于QtWidget开发的,目前实现了大约20个Material风格的组件,下面教大家如何编译该组件......
  • Nexus 为 Visual Studio 提供 Nuget 代理
    现在绝大多数企业,为了保护公司利益,都会在内网进行代码开发,内网肯定是无法上网的。对于.net来说,如果能够使内网的机器,通过Nuget下载到外网的第三方类库,是一件很重要的事......
  • curl [Get]
    项目中经常会有测试线上或者测试环境非本地的接口的数据结构或者返回信息是什么,提前规划字段或者结构,那如何实现呢?这里仅使用get方法获取示例如下:curl-A'Mozilla/5.0......
  • GET 请求与POST 请求
    GET请求携带数据<!DOCTYPEhtml><html><head><metacharset="utf-8"><title></title></head><body><!--<formid="l......
  • Avalonia 11.0-preview6中的一些更新
    1.DirectProperty->StyledProperty有相当多的DirectProperty变成了StyledProperty,对于已经有的应用没用什么实质影响,但是此后有些本来不可以在Style中设置的属性也可以在......
  • .NET Core 3.0-preview3 发布
    .NETCore3.0Preview3已经发布,框架和ASP.NETCore有许多有趣的更新。这是最重要的更新列表。下载地址:​​https://aka.ms/netcore3download​​。​​.NETCore3.0......
  • 微信小程序之wx.getLocation再次授权问题解决
    首先,在page外定义一个公共函数用于发送获取位置的请求vargetLocation=function(that){wx.getLocation({type:'wgs84',success:function(res){......
  • getLocation小程序获取用户地理位置,逆地址解析(位置描述),将经纬度转为省市区的位置描述
    在近期做得一个小程序里要展示当地天气情况,这也就意味着要获取当前位置信息,小程序这边需要getLocation来向用户发起弹窗申请授权获取位置信息。就像下面这样: 不过呢,现在......