2

What is the most efficient way to get a single, random, 5 star review (from any/all products).

I'm trying to display a single random review on our homepage. Here's what I'm working with currently but it has several issues.

$review = Mage::getModel('review/review');
$collection = $review->getProductCollection();
$collection
    ->addAttributeToSelect('*')
    ->getSelect()
            ->limit(5)
            ->order('rand()');
$review->appendSummary($collection);

foreach($collection as $product) {
$ratingSummary = $product->getRatingSummary();
$starRating = $ratingSummary['rating_summary'];

if($starRating == 100) {
    echo "<h1>". $product->getDetail() ."</h1>";
}
2

Try something like this:

$reviews = Mage::getModel('review/review')
    ->getResourceCollection()
    ->addStoreFilter(Mage::app()->getStore()->getId())
    ->addStatusFilter(Mage_Review_Model_Review::STATUS_APPROVED)
;

$reviews->getSelect()
    ->joinInner(array('rs' => 'review_entity_summary'), 'rs.review_id = main_table.entity_id')
    ->where('rs.rating_summary = 100')
    ->order(new Zend_Db_Expr('rand()'))
;

$randomReview = $reviews->getFirstItem();

LIMIT is not needed here as you are displaying only one record.

Be aware that RAND() is not the best idea if you have a lot of records.

| improve this answer | |
  • Thanks for the reply. There seems to be an issue with joinInner:: Warning: Missing argument 2 for Zend_Db_Select::joinInner(), called in /home/vagrant/app/design/frontend/sm-fcstore/yogavibes/template/randomtestimonial/random_testimonial.phtml on line 46 and defined in /home/vagrant/app/code/core/Zend/Db/Select.php on line 350 – Andrew Boehm Aug 10 '16 at 17:17
  • @AndrewBoehm there was mistake in code, looks ok now – rafal-kos Aug 10 '16 at 17:19
  • That seems to have resolved the issue in the system.log however the code is still failing and causing the entire content block not to display. The issue is resolved if I remove ->joinInner and ->where. Thanks again for your time – Andrew Boehm Aug 10 '16 at 17:31
  • Any ideas on whats causing the join to fail? @rafal-kos – Andrew Boehm Aug 10 '16 at 19:55
  • I really want to accept this answer as it seems like the proper method however something is not working with joinInner. It fails without error so I'm not sure what is wrong. Without it the code returns a random review (just not the 5 star review I'm looking for). – Andrew Boehm Aug 15 '16 at 14:29
1

Based on Rafal-Kos suggestion. Needed to update tables and columns:

    $reviews = Mage::getModel('review/review')
    ->getCollection()
    ->addStoreFilter(Mage::app()->getStore()->getId())
    ->addStatusFilter(Mage_Review_Model_Review::STATUS_APPROVED)
;

$reviews->getSelect()
    ->joinInner(array('rs' => 'rating_option_vote'), 'rs.review_id = main_table.review_id')
    ->where('rs.percent = 100')
    ->order(new Zend_Db_Expr('rand()'))
;

$randomReview = $reviews->getFirstItem();
| improve this answer | |
  • why not just accept the answer provided by RafalKos if that helped you solve the problem. – Marius Aug 16 '16 at 21:08
  • It didn't feel right accepting an answer with code that didn't work....but I've switched my acceptance since it was his insight that helped. – Andrew Boehm Aug 25 '16 at 17:48

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.