This is the documentation for LemonStand V1, which has been discontinued. You can learn more and upgrade your store here.

LemonStand Version 1 Has Been Discontinued

This documentation is for LemonStand Version 1. LemonStand is now offered as a cloud-based eCommerce platform.
You can try the new LemonStand and learn about upgrading here.

Displaying a List of Product Reviews

Displaying a list of product reviews

There are two methods in the Shop_Product class which allow you to fetch product reviews:

  • list_reviews() - returns only approved reviews.
  • list_all_reviews() - returns all reviews, including non-approved.

Both methods return a collection (Db_DataCollection) of reviews. Each element in the collection is an object of the Shop_ProductReview class.

To display a list of reviews you can use the following code:

<?
  $reviews = $product->list_all_reviews();
  if (!$reviews->count):
?>
  <p>There are no reviews for this product.</p>
<? else: ?>
  <ul>
    <? foreach ($reviews as $review): ?>
    <li>
      <? if ($review->rating): ?>
        <span class="rating_stars_small rating_<?= $review->rating ?>"><?= $review->rating ?></span>
      <? endif ?>
      <h4><?= h($review->title) ?></h4>
      <p class="description">Posted by <?= h($review->author) ?> on <?= $review->created_at->format('%x') ?></p>
      <p><?= nl2br(h($review->review_text)) ?></p>
    </li>
    <? endforeach ?>
  </ul>
<? endif ?>
{% set reviews = product.list_all_reviews() %}
{% if not reviews.count %}
  <p>There are no reviews for this product.</p>
{% else %}
  <ul>
    {% for review in reviews %}
      <li>
        <span class="rating_stars_small rating_{{ review.rating }}">{{ review.rating }}</span>
        <h4>{{ review.title }}</h4>
        <p class="description">Posted by {{ review.author }} on {{ review.created_at.format('%x') }}</p>
        <p>{{ review.review_text|nl2br }}</p>
      </li>
    {% endfor %}
  </ul>
{% endif %}

This code outputs a list of reviews in the following format:

Each review can have a rating. You can output review ratings as numbers, or using the CSS method described earlier. Below are CSS rules which we use for styling review ratings in the LemonStand demo store.

span.rating_stars_small
{
  display: block;
  width: 73px;
  height: 15px;
  text-indent: -100000em;
  text-align: left;
  background: transparent url(../images/rating_stars_small.png) no-repeat -3px -135px;
  margin: 8px 0;
}

span.rating_stars_small.rating_1 {background-position: -3px -120px;}
span.rating_stars_small.rating_2 {background-position: -3px -90px;}
span.rating_stars_small.rating_3 {background-position: -3px -60px;}
span.rating_stars_small.rating_4 {background-position: -3px -30px;}
span.rating_stars_small.rating_5 {background-position: -3px top;}

The image:


Previous: Displaying Product Ratings and Reviews
Return to Displaying Product Ratings and Reviews