VirtueMart Forum

VirtueMart Dev/Coding Central: VM1 (old version) => Development Projects, Modifications, Hacks & Tweaks. VM1.1 => Topic started by: Wolverine on April 21, 2007, 03:58:31 am

Title: Product Review Count method for ps_reviews
Post by: Wolverine on April 21, 2007, 03:58:31 am
This is probably more of an enhancement to the core rather than a hack.  Basically, in the last month I have done quite a bit of modifications to virtuemart to satisfy the needs of a specific wireframe laid out by a designer.  So I think I will try and post as much as I can of the modifications I made in hopes that the virtuemart core will be improved.

This modification simply abstracts the query and retrieval of the product review count from the ps_reviews::product_reviews method.  This allows someone to retrieve the number of reviews without actually retrieving the reviews themselves.

In ps_reviews.php add this method:
Code: [Select]
/**
 * Returns the number of reviews for this product ID
 *
 * @param string $product_id Count the reviews of this product ID
 * @return integer Zero in the absence of reviews for this product ID
 */
function countReviews($product_id)
{
    if (PSHOP_ALLOW_REVIEWS == "1" ) {
  $dbc = &new ps_DB;
          $query = "SELECT COUNT(*) AS num_rows "
                     . "FROM #__{vm}_product_reviews "
                     . "WHERE product_id='$product_id'";
  $dbc->query($query);
  $num_rows = $dbc->f('num_rows');
  if (!empty($num_rows)) {
      return (int)$num_rows;
  }
    }     
    return 0;
}

Then modify the method ps_reviews::product_reviews to use this new method ps_reviews::countReviews().  To do this commend or remove the following lines of code from the method ps_reviews::product_reviews:
Code: [Select]
$count = "SELECT COUNT(*) as num_rows FROM #__{vm}_product_reviews WHERE product_id='$product_id'";
Code: [Select]
$dbc->query( $count );

Next, modify the following line to use the new method by changing it from this:
Code: [Select]
$num_rows = $dbc->f('num_rows');
to be this:
Code: [Select]
$num_rows = ps_reviews::countReviews($product_id);

Now if you only need to know the number of reviews for any given product it is easily retrievable.  This is helpful for templating so that you only show a review section *if* there are actually reviews for the product.