获取商品的评论列表在商城中也是十分常见也是必须的功能,这里我介绍自己写的获取商品评价的方法,代码如下:
$review = Mage::helper('fun_review');
$productReviewList = $review->getReviewListByProductId($productId, 1, $page_index, $page_size);
这里根据商品是否是可配置(configurable)商品进而使用不同的方法获取review列表
/**
* 获取商品的评论列表
*
* @param $productId
* @param int $status
* @param int $page
* @param int $pageSize
* @return mixed
*/
public function getReviewListByProductId($productId, $status = 1, $page = 1, $pageSize = self::REVIEW_PAGE_SIZE)
{
$_product = Mage::getModel('catalog/product')->load($productId);
$type_id = $_product->getTypeId();
$review = Mage::getResourceModel('review/review_collection');
//->addStoreFilter(Mage::app()->getStore()->getId()); //store_id Filter
$reviewDetailTable = $review->getTable('review/review_detail');
$review->getSelect()->joinInner([
'd' => $reviewDetailTable
], "d.review_id = main_table.review_id", ['detail_id', 'customer_id']);
if($type_id == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) {
$childProductIds = Mage::getModel('catalog/product_type_configurable')
->getChildrenIds($productId);
$reviewList = $review->addFieldToFilter('entity_pk_value', array('in' => $childProductIds))
->addFieldToFilter('status_id', $status)
->setOrder('review_id', 'DESC')
->setPageSize($pageSize)
->setCurPage($page)
->getItems();
} else {
$reviewList = $review->addFieldToFilter('entity_pk_value', array('eq' => $productId))
->addFieldToFilter('status_id', $status)
->setOrder('review_id', 'DESC')
->setPageSize($pageSize)
->setCurPage($page)
->getItems();
}
return $reviewList;
}