我需要在类别上显示评分。
我正在考虑从类别中的产品中获得该等级并进行平均。
似乎“很重”,可能会降低网站速度。
还有其他办法吗?
答案 0 :(得分:0)
我最近解决了这个问题。在我给你代码后,我先解释一下:)。
事实上,第一个问题是优化。我解决的问题是,我将评级存储在数据库中,并使用cron job.its尚未在线,但它会每晚运行。
首先,我采用了所有子类别(我不需要根类别,但如果你这样做,你会从代码中找出如何获得它们) - >我检查了所有类别的产品,检查每个产品的评级 - >将百分比添加到数组 - >计算平均值 - >写/更新(取决于是否有)评论(以编程方式) - >添加/更新评级到评论(以编程方式) - >重复所有类别。
include ('app/Mage.php');
Mage::app();
$_helper = Mage::helper('catalog/category');
$_categories = $_helper -> getStoreCategories();
$cateogory_ids = array();
if (count($_categories) > 0) :
foreach ($_categories as $_category) :
$_category = Mage::getModel('catalog/category') -> load($_category -> getId());
$_subcategories = $_category -> getChildrenCategories();
if (count($_subcategories) > 0) :
foreach ($_subcategories as $_subcategory) :
$cateogory_ids[] = $_subcategory; // get all the subcategories as objects
endforeach;
endif;
endforeach;
endif;
$review_per_category = array();
$store_id = Mage::app() -> getStore() -> getId();
foreach ($cateogory_ids as $category) {
$collection = $category -> getProductCollection();
$collection -> addAttributeToSelect('*');
foreach ($collection as $product) {
$summaryData = Mage::getModel('review/review_summary') -> setStoreId($store_id) -> load($product -> getEntityId());
$review_per_category[] = $summaryData['rating_summary']; // get the rating of all the products from this category
}
$review_per_category = array_filter($review_per_category);
$count = count($review_per_category);
$sum = array_sum($review_per_category);
$total = $sum / $count;
$review = Mage::getModel('review/review')->load($category -> getId(),'entity_pk_value'); // try to load the category review,if there is -> update else -> create
$total_rating_option = round($total / 20); // rating percentage
if(trim($review->getReviewId()) == ""){
// create review and rating branch;
echo $category -> getId().' review not found.Writing review<br/>';
$_review = Mage::getModel('review/review')
->setEntityPkValue($category->getId())
->setStatusId(1)
->setTitle($category -> getName().' Category Review')
->setDetail($category -> getName().' Category Review')
->setEntityId(3)
->setStoreId($store_id)
->setNickname('Auto')
->save();
$rating_options = array(
1 => array(1,2,3,4,5),
2 => array(6,7,8,9,10),
3 => array(11,12,13,14,15),
4 => array(16,17,18,19,20) // we use custom rating options as 4.but the theory is the same. the 4th is kind of the sum of the first 3.
);
foreach($rating_options as $rating_id => $option_ids){
if($rating_id != 4)
continue;
try {
if($_review -> getId() != ""){
$_rating = Mage::getModel('rating/rating')
->setRatingId(4)
->setReviewId($_review->getId())
->setPercent($total)
->addOptionVote($option_ids[$total_rating_option-1],$category->getId());
}
} catch (Exception $e) {
die($e->getMessage());
}
}
}else{
// update rating branch;
$rating_options = array(
1 => array(1,2,3,4,5),
2 => array(6,7,8,9,10),
3 => array(11,12,13,14,15),
4 => array(16,17,18,19,20)
);
$percent = array(
16 => 1,
17 => 2,
18 => 3,
19 => 4,
20 => 5,
);
$rating = Mage::getModel('rating/rating_option_vote') -> load($category->getId(),'entity_pk_value');
foreach($rating_options as $rating_id => $option_ids):
if($rating_id != 4)
continue;
try {
$rating -> setOptionId($option_ids[$total_rating_option-1],$category -> getId())
-> setPercent($total)
-> setValue($percent[$option_ids[$total_rating_option-1]])
-> save()
;
} catch (Exception $e) {
die($e->getMessage());
}
endforeach;
}
$review_per_category = array();
}
当然,我自己并没有做到这一切。 如果您有任何问题,请随时问我。 祝你好运! :)
您应该检查更改的表格是&quot; rating_option_vote&#39;和&#39; review_detail&#39;