magento根据产品评级计算类别的平均评级
我需要显示类别的评级 我想从产品类别中获得评级,并得出平均值 这似乎是'沉重的',它可能会减慢网站的速度magento根据产品评级计算类别的平均评级,magento,categories,rating,Magento,Categories,Rating,我需要显示类别的评级 我想从产品类别中获得评级,并得出平均值 这似乎是'沉重的',它可能会减慢网站的速度 还有其他方法吗?我最近解决了这个问题。在我给你代码后,我将首先解释: 1.存储 事实上,第一个问题是优化。我如何解决这个问题的是,我使用cron作业将评级存储在数据库中。它还没有联机,但它将每晚运行 2.精明的 首先,我把所有的子类别,我不需要根类别,但如果你需要,你会从代码中找到如何获得它们->我检查了每个产品的所有类别产品的评级->将百分比添加到数组->计算平均值->写入/更新取决于是否
还有其他方法吗?我最近解决了这个问题。在我给你代码后,我将首先解释: 1.存储 事实上,第一个问题是优化。我如何解决这个问题的是,我使用cron作业将评级存储在数据库中。它还没有联机,但它将每晚运行 2.精明的 首先,我把所有的子类别,我不需要根类别,但如果你需要,你会从代码中找到如何获得它们->我检查了每个产品的所有类别产品的评级->将百分比添加到数组->计算平均值->写入/更新取决于是否有一个ReviewProgrammatically->将/更新评级添加到ReviewProgrammatically->重复所有类别 3.代码 4.总结 当然,这些都不是我一个人做的。 如果你有任何问题,请随时问我。 祝你好运 4.1. 数据库 您应该检查更改的表格是“评级、选项、投票”和“审查、详细信息”
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();
}