Thanks for your article.
I have implement and i want to share with everyone about "rating fiter" feature
Step 1 : create a Extension.
app/code/local/Packages/RatingFilter/etc/config.xml
<config>
<modules>
<Packages_RatingFilter>
<version>0.1.0</version>
</Packages_RatingFilter>
</modules>
<global>
<models>
<ratingfilter>
<class>Packages_RatingFilter_Model</class>
</ratingfilter>
</models>
<resources>
<ratingfilter_setup>
<setup>
<module>Packages_RatingFilter</module>
<class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</ratingfilter_setup>
</resources>
<events>
<review_save_after>
<observers>
<Packages_RatingFilter_Model_Observer>
<type>singleton</type>
<class>Packages_RatingFilter_Model_Observer</class>
<method>updateRatingFilterAttribute</method>
</Packages_RatingFilter_Model_Observer>
</observers>
</review_save_after>
</events>
</global>
Step2: Setup attribute
app/code/local/Packages/RatingFilter/sql/ratingfilter_setup/mysql4-install-0.1.0.php
$installer = $this;
$installer->startSetup();
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'rating_filter', [
'group' => 'General',
'label' => 'Rating',
'type' => 'int',
'input' => 'select',
'default' => '',
'class' => '',
'backend' => '',
'frontend' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => false,
'required' => false,
'searchable' => false,
'filterable' => true,
'filterable_in_search' => true,
'comparable' => false,
'visible_on_front' => false,
'visible_in_advanced_search' => true,
'unique' => false,
'user_defined' => 1,
'option' => [
'values' => [
1 => 'One star',
2 => 'Two stars',
3 => 'Three stars',
4 => 'Four stars',
5 => 'Five stars',
]
],
]);
$installer->endSetup();
Step3: create observer
class Packages_RatingFilter_Model_Observer
{
public function updateRatingFilterAttribute(Varien_Event_Observer $observer) {
$object = $observer->getEvent()->getObject();
$data = $object->getData();
$newRatings = $data['ratings'];
$reviewId = $data['review_id'];
$newSumRatings = 0;
foreach ($newRatings as $r) {
$value = $r % 5;
$newSumRatings += ($value) ? $value : 5;
}
$statusId = $object->getStatusId();
if ($statusId == 1) {
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$productId = $object->getEntityPkValue();
$product = Mage::getModel('catalog/product')->load($productId);
$reviews = Mage::getModel('review/review')
->getResourceCollection()
->addStoreFilter(Mage::app()->getStore()->getId())
->addEntityFilter('product', (int) $productId)
->addStatusFilter(Mage_Review_Model_Review::STATUS_APPROVED)
->setDateOrder()
->addRateVotes();
$avg = 0;
$sum = 0;
if (count($reviews) > 0) {
foreach ($reviews->getItems() as $review) {
$temp = 0;
if ($reviewId == $review->getReviewId()) {
$sum += $newSumRatings;
} else {
foreach ($review->getRatingVotes() as $vote) {
$temp += $vote->getPercent() / 20;
}
$sum += $temp;
}
}
$rating = $sum / (count($reviews));
}
if ($rating == 5) {
$product->setRatingFilter(24); // option id
$product->save();
} elseif ($rating >= 4) {
$product->setRatingFilter(23);
$product->save();
} elseif ($rating >= 3) {
$product->setRatingFilter(22);
$product->save();
} elseif ($rating >= 2) {
$product->setRatingFilter(21);
$product->save();
} else {
$product->setRatingFilter(20);
$product->save();
}
}
}
}