Displaying Random Reviews Outside of Magento Review Module
Magento offers robust review functionality for products right out-of-the-box. However, pulling a random product with an associated review into a sidebar template isn’t straightforward due to the modular architecture. Let’s explore how to achieve this using Model loading and custom block creation.
Understanding the Challenge
In your case, you’ve managed to display a random product in the sidebar but are struggling to:
- Filter products based on whether they have an associated review.
- Load and display the summary of that review within the sidebar template.
The provided code snippet attempts to load a review summary for a given product ID using the Mage::getModel() function. However, this is throwing a “Call to non-object” error because the review/review_summary model does not exist in Magento 1.5 (it was introduced later).
Step-by-Step Solution
1. Creating a Custom Block
First, let’s create a new block that will handle fetching and displaying random products with reviews:
<?php
class Company_Laravel_Block_Sidebar_Randomreviews extends Mage_Core_Block_Template
{
public function getRandomProductWithReview()
{
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect(array('name', 'review_count'))
->setPageSize(1)
->setCurPage(1);
foreach ($products as $product) {
if ($product->getReviewCount() > 0) { // Only consider products with reviews
return $product;
}
}
return null; // Return null if no product with review is found
}
}
2. Loading the Review Summary
To load and display a summary of the review, you’ll need to use the Mage_Review_Model_Review model:
<?php
class Company_Laravel_Model_Sidebar_Randomreviews
{
public function getProductReviews($productId)
{
$reviewCollection = Mage::getModel('review/review')
->getCollection()
->addFilter('entity_pk_value', $productId);
if ($reviewCollection->count() > 0) {
return $reviewCollection;
}
return null; // Return null if no reviews are found
}
}
3. Displaying in Sidebar
In your sidebar template, you can now display the random product and its review summary:
<!-- app/design/frontend/default/package/template/catalog/product/quickview.phtml -->
<?php $randomProduct = $this->getRandomProductWithReview(); ?>
<?php if ($randomProduct && $reviews = Mage::_helper('review')->getItemCollection()->addEntityFilter($randomProduct)->setPageSize(1)): ?>
<div class="random-product-review">
<!-- Display product name and image here -->
<?php echo $this->htmlEscape($randomProduct->getName()); ?>
<img src="<?php echo $this->helper('catalog/image')->init($randomProduct, 'small_image')->resize(100); ?>" alt="<?php echo $this->htmlEscape($randomProduct->getName()) ?>">
<!-- Display review summary -->
<?php foreach ($reviews as $review) : ?>
<p class="review-summary"><?php echo $review->getData('detail') ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
Conclusion
Displaying a random product with its review in the sidebar of your Magento store requires a combination of custom block creation, model loading, and template manipulation. By following this approach, you can effectively retrieve and display the desired content without having to modify core files.
Remember, it’s always recommended to use clean, efficient code and adhere to best practices when working with Magento extensions. If you need further assistance or more advanced solutions, feel free to explore our services at Laravel Company.