8

I am using Magento 1.8 version. I have written an observer that has to run when admin save a product review with status "Approved". And i have created a ratings attribute for every product with multiselect and it should automatically update every time when admin save the review for a product.

Note: Reviews are in Reports/Reviews/Product Reviews.

etc/Config.xml Code:

<review_save_after>
    <observers>
        <efkadminhtml>
            <class>efkadminhtml/observer</class>
            <method>ratingsUpdate</method>
        </efkadminhtml>
    </observers>
</review_save_after>

mode/Observer Code

public function ratingsUpdate(Varien_Event_Observer $observer)
{
    $object = $observer->getEvent()->getObject();
    $statusId = $object->getStatusId();

    if($statusId == 1) {
        $common = Mage::getSingleton('catalog/common');
        $attribute = $common->getAttribute('ratings');
        Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

        $productId = $object->getEntityPkValue();
        $product = Mage::getModel('catalog/product')->load($productId);
        $avgRating = Mage::getBlockSingleton('efkreports/product_allReviews')->getAvgReview($product->getId());

        Mage::log($avgRating);
        Mage::log($attribute->getOptionId(round($avgRating)));
        $product->setRatings($attribute->getOptionId(round($avgRating)));
        //$product->setRatings(round($avgRating));
        $product->save();
    }
}

When admin is attempting to save review for a product, the above observer code is executing and review is updating correctrly but product is getting previous review value rather than latest updated review.

I wrote <review_save_after> but it getting previous value. What is the event called after "Save Review" Is that correct or not?

Please tell me where I am wrong.

Thanks in advance.

4

After lot of research I got solution...:-) But we may do this very easily.

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;
        }
        $newAvgRating = $newSumRatings;

        $statusId = $object->getStatusId();
        if($statusId == 1) {
            $common = Mage::getSingleton('catalog/common');
            $attribute = $common->getAttribute('ratings');
            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;

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

                $avg = $sum / (count($reviews) * 3);
            }

            $product->setRatings($attribute->getOptionId(round($avg)));
            $product->save();
        }
    }
1
<review_save_after>
 <observers>
    <efkadminhtml>
        <type>singleton</type> 
        <class>efkadminhtml/observer</class>
        <method>ratingsUpdate</method>
    </efkadminhtml>
</observers>

add Type and then Try

  • I have put that line <type>singleton</type>. But no effect still getting same problem. – Sivakumar Dec 23 '13 at 14:01
  • try to log using Mage::log('test'). and please let me know – Keyul Shah Dec 23 '13 at 14:35
  • Mage::log('test') is displaying test. Observer function is executing. – Sivakumar Dec 23 '13 at 14:38
  • Then Your observer is working fine – Keyul Shah Dec 23 '13 at 14:41
  • 2
    letsmakeitgo.com/blog/2011/06/… follow this link for coding – Keyul Shah Dec 24 '13 at 2:56

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.