5

I want to implement Rating Filter in layered navigation sidebar. But I have no idea how to proceed or where to begin.. Read this article.. got little idea but still don't know to implement rating filtration. Somebody can guide me on this?

EDIT: After searching a lot I came across this. Then I made an attribute Rating with dropdown value of 1,2,3,4,5. Now Whenever I am setting the value with observer it is setting value with the product(checking using $product->getData()) but when I am checking in the product edit(adminhtml) is not setting the value.

Also I have only one rating instead of 3( Have only Overall rating). So How I am suppose to get only avg rating of one product.

here'S MY Current code:

 public function ratingsUpdate(Varien_Event_Observer $observer) {
        $object = $observer->getEvent()->getObject();
        $data = $object->getData();
        $newRatings = $data['ratings'];
        $reviewId = $data['review_id'];

        $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);
            $summaryData = Mage::getModel('review/review_summary')
                                        ->load($productId);

        if ($summaryData['rating_summary'] == 90):
           // Mage::log($summaryData['rating_summary'], null, 'size1.log', true);
            $product->setRating(5);//
            $product->save();
        endif;
    }
2
public function ratingsUpdate(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->setRatings(11); //
                $product->save();
            } elseif ($rating >= 4) {
                $product->setRatings(12); //
                $product->save();
            } elseif ($rating >= 3) {
                $product->setRatings(13); //
                $product->save();
            } elseif ($rating >= 2) {
                $product->setRatings(14); //
                $product->save();
            } else {
                $product->setRatings(15); //
                $product->save();
            }

        }
    }
2

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();
            }

        }
    }
}
  • @ Haris, where to create observer code file location can you tell me? – Vijayakumar Jun 22 '17 at 5:19
  • I want to implement this model to my store, pls tell me – Vijayakumar Jun 22 '17 at 5:20
  • I was created observer file "app/code/local/Packages/RatingFilter/Model/Observer/Observer.php" – Vijayakumar Jun 22 '17 at 6:18
  • there is no change in layered navigation? – Vijayakumar Jun 22 '17 at 6:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.