Xand94
Xand94

Reputation: 717

Magento - displaying a random review outside of review module

I am using Magento 1.5 and am attempting to include a small box in my sidebar for a random product showing a product image, product name, star rating and part of a review.

I have managed to get Magento displaying a random product in the sidebar, unfortunately I cannot seem to find a way to select the random product based on if it has a review or not, and I also cannot find a way to load a review summary into the sidebar template I'm using.

I found the following piece of example code but it just throws the good old call to non object error.

<?php

$storeId    = Mage::app()->getStore()->getId();

$summaryData = Mage::getModel(‘review/review_summary’)
->setStoreId($storeId)
->load($_product->getId());

/* @var $summaryData Mage_Review_Model_Review_Summary */

/*

array(
['primary_id'] => 147
['entity_pk_value'] => 166
['entity_type'] => 1
['reviews_count'] => 1
['rating_summary'] => 80
['store_id'] => 1
)
*/

?>

if anyone has any ideas how to get this working, it would be greatly appreciated.

Thanks in advance!

Upvotes: 3

Views: 3615

Answers (3)

Daniel Sloof
Daniel Sloof

Reputation: 12706

The reason you are getting that error is because of the quotes you are using. Use ' instead.

In response to your edit, this is how you would go ahead and load 5 random products that have a review:

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

foreach($collection as $product) {
        var_dump($product->debug());
}

Obviously now you can do something like:

$product->getRatingSummary()

to extract rating data, etc.

Of course it is up to you to create a block to put this in (or the bad way -> just put it in some template).

Have fun ;)

Third edit in response to your question:

/* Getting summary title / body. */
$title = $product->getTitle();
$body  = $product->getDetail();

/* To get (what I assume is) 'star' rating. */
$ratingSummary = $product->getRatingSummary();
$starRating = $ratingSummary['rating_summary'];

Upvotes: 10

clockworkgeek
clockworkgeek

Reputation: 37700

You are probably looking for Mage_Review_Model_Review::getEntitySummary()

For simplicity Magento already provides a product collection cross referenced with reviews.

$products = Mage::getResourceModel('review/review_product_collection');
$products->getSelect()->order(new Zend_Db_Expr('RAND()'));
$products->setPageSize(3);
foreach ($products as $product) {
    // Picks 3 random products with reviews.
    Mage::getSingleton('review/review')->getEntitySummary($product, $storeId);
    $summary = $product->getRatingSummary();
    // $summary->getRatingSummary() = percentage of average rating values
}

Upvotes: 1

Sascha S.
Sascha S.

Reputation: 61

Instead of selecting a product by random and showing a review, i would rather select a review and load its related product. This has 2 effects: 1. You automatically have a product that has a review. 2. Products with more reviews have an increased probability of being shown.

But thats just me ^^

Upvotes: 2

Related Questions