0

i am trying to develop a custom module regarding reviews, reviews are shown in a list according to votes given to it by customers, for that purpose i altered the magento review table and inserted a new field called votes.

now the problem is that i am trying to sort the reviews according to the votes assigned to it but the reviews are ordered in the last updated review i.e the review which got last vote regardless of positive or negative, is shown on the top. my code;

$this->_reviewsCollection = Mage::getModel('review/review')->getCollection()
                    ->addStoreFilter(Mage::app()->getStore()->getId())
                    ->addStatusFilter(Mage_Review_Model_Review::STATUS_APPROVED)
                    ->addEntityFilter('product', $this->getProduct()->getId())
                    //->addOrder('votes','DESC');
                    //->setOrder('votes', 'DESC');
                    //$_reviewsCollection->getSelect()->order('main_table.votes DESC');
                    //->setDateOrder();
                    ->unshiftOrder('votes', 'DESC')

the different sorting methods i tried are also here (commented) Thanks in advance.

2 Answers 2

2

At last solved with the help of my great "sirjee", just used array sorting in list.phtml file. here what i did.

<?php
        $votes = array();
        foreach ($_items as $key => $row)
        {
            $votes[$key] = $row['votes'];
        }
        array_multisort($votes, SORT_DESC, $_items);
         ?>

nothing more

1
  • That's just a workaround, not a solution to the problem. But if it's suits you I guess it's fine. Commented May 20, 2014 at 15:05
0

Please be aware that to be able to order a collection on a field, you need to include that field to the collection, as it is restricted to a limited number of fields (id and some others) if you don't specify '*'.

try something like this

$this->_reviewsCollection = Mage::getModel('review/review')->getCollection()  
                            ->addStoreFilter(Mage::app()->getStore()->getId())  
                            ->addStatusFilter(Mage_Review_Model_Review::STATUS_APPROVED)
                            ->addEntityFilter('product', $this->getProduct()->getId())
                            ->addAttributeToSelect('votes')
                            ->setOrder('votes', 'DESC');

in case ->addAttributeToSelect('votes') doesn't work, try ->addFieldToSelect('votes')

1
  • Sorry, but it didn't worked for me. still facing the same problem Commented May 20, 2014 at 5:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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