Important Notice from AspDotNetStorefront
It is with dismay that we report that we have been forced, through the action of hackers, to shut off write-access to this forum. We are keen to leave the wealth of material available to you for research. We have opened a new forum from which our community of users can seek help, support and advice from us and from each other. To post a new question to our community, please visit: http://forums.vortx.com
Results 1 to 3 of 3

Thread: Rating Stars by the SKU & on the Cat pages!

  1. #1
    dragon is offline Member
    Join Date
    Feb 2009
    Posts
    66

    Default Rating Stars by the SKU & on the Cat pages!

    Hi...I'm looking for a way to put the just the rating stars under each product in my category pages. I would also like just the stars by the sku on the product pages. Any ideas? Thank you for your time.

  2. #2
    DanV's Avatar
    DanV is offline Ursus arctos horribilis
    Join Date
    Apr 2006
    Posts
    1,568

    Default

    You could write an extension function in XSLTExtensionBase that handled this...

    Eg...

    Code:
            public string EntityPageRatings(String sProductID)
            {
                InputValidator IV = new InputValidator("EntityPageRatings");
                int ProductID = IV.ValidateInt("ProductID", sProductID);
    
                StringBuilder RatingHTML = new StringBuilder();
    
                // Make sure there are ratings to display
                int NumberOfRatings = DB.GetSqlN("select count(*) as N from Rating where IsFilthy = 0  and ProductID = " + ProductID.ToString());
    
                if (NumberOfRatings > 0)
                {
                    //We have ratings, so lets build the html
                    int RatingSum = DB.GetSqlN("select SUM(rating) as N from Rating where IsFilthy = 0 and ProductID =" + ProductID.ToString());
                    int i = 1;
    
                    decimal AvgRating = 0.00M;
    
                    //Calculate the Average rating here, as SQL AVG function handles data types inconsistently
                    if (RatingSum > 0)
                    {
                        //DON'T DIVIDE BY ZERO!
                        AvgRating = (Convert.ToDecimal(RatingSum) / Convert.ToDecimal(NumberOfRatings));
                    }
    
                    //First build the "whole" stars
                    while (i <= AvgRating)
                    {
                        RatingHTML.Append("<img src=\"./images/starf.gif\" />");
                        i++;
                    }
    
                    //Now add a partial star in case the average is something like 3.5
                    if (AvgRating > (i - 1))
                    {
                        RatingHTML.Append("<img src=\"./images/starh.gif\" />");
                        i++;
                    }
    
                    //Add the "empty" stars
                    while (i <= 5)
                    {
                        RatingHTML.Append("<img src=\"./images/stare.gif\" />");
                        i++;
                    }
    
                    //Finally, represent the rating with text as well
                    RatingHTML.Append("<br/><span class=\"AvgRatingText\">Average Customer Rating:<br/>" + AvgRating.ToString() + " out of 5</span>");
                }
                else
                {
                    //Just display a message stating that the product has not been reviewed
                    RatingHTML.Append("<span class=\"NoRatingsText\">No Reviews</span>");
                }
                return RatingHTML.ToString();
            }
    and then call the extension function from the ProductCell template in the entity.grid xml package like so:

    Code:
    <xsl:if test="aspdnsf:AppConfigBool('RatingsEnabled') = 'true'">
        <tr>
            <td height="45" align="center" valign="top">
                <xsl:value-of select="aspdnsf:EntityPageRatings(ProductID)" disable-output-escaping="yes" />
            </td>
        </tr>
    </xsl:if>
    And what you end up with is something that looks like:

    Last edited by DanV; 03-27-2009 at 12:12 PM. Reason: clean up XML package code formatting

  3. #3
    George the Great is offline Senior Member
    Join Date
    Nov 2006
    Location
    Cleveland, OH
    Posts
    1,792

    Default

    No source...no worries You can do this with a custom extension class in the App_Code directory and a couple small changes to the xmlpackage and web.config file.

    App_Code/CustomExtensions.cs
    Code:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Text;
    using AspDotNetStorefrontCore;
    
    
    /// <summary>
    /// Summary description for CustomExtensions
    /// </summary>
    ///
    namespace mycustomextensions
    { 
        public class CustomExtensions
        {
            public string EntityPageRatings(String sProductID)
            {
                InputValidator IV = new InputValidator("EntityPageRatings");
                int ProductID = IV.ValidateInt("ProductID", sProductID);
    
                StringBuilder RatingHTML = new StringBuilder();
    
                // Make sure there are ratings to display
                int NumberOfRatings = DB.GetSqlN("select count(*) as N from Rating where IsFilthy = 0  and ProductID = " + ProductID.ToString());
    
                if (NumberOfRatings > 0)
                {
                    //We have ratings, so lets build the html
                    int RatingSum = DB.GetSqlN("select SUM(rating) as N from Rating where IsFilthy = 0 and ProductID =" + ProductID.ToString());
                    int i = 1;
    
                    decimal AvgRating = 0.00M;
    
                    //Calculate the Average rating here, as SQL AVG function handles data types inconsistently
                    if (RatingSum > 0)
                    {
                        //DON'T DIVIDE BY ZERO!
                        AvgRating = (Convert.ToDecimal(RatingSum) / Convert.ToDecimal(NumberOfRatings));
                    }
    
                    //First build the "whole" stars
                    while (i <= AvgRating)
                    {
                        RatingHTML.Append("<img src=\"./images/starf.gif\" />");
                        i++;
                    }
    
                    //Now add a partial star in case the average is something like 3.5
                    if (AvgRating > (i - 1))
                    {
                        RatingHTML.Append("<img src=\"./images/starh.gif\" />");
                        i++;
                    }
    
                    //Add the "empty" stars
                    while (i <= 5)
                    {
                        RatingHTML.Append("<img src=\"./images/stare.gif\" />");
                        i++;
                    }
    
                    //Finally, represent the rating with text as well
                    RatingHTML.Append("<br/><span class=\"AvgRatingText\">Average Customer Rating:<br/>" + AvgRating.ToString() + " out of 5</span>");
                }
                else
                {
                    //Just display a message stating that the product has not been reviewed
                    RatingHTML.Append("<span class=\"NoRatingsText\">No Reviews</span>");
                }
                return RatingHTML.ToString();
            }
        }
    }
    web.config (new code is red code)
    Code:
    <!-- Add your own custom XSLTExtensionObjects here                                                                                        -->
    <xsltobjects defaultExtension="">
        <extensions>
            <clear />
            <add name="receipt" namespace="urn:receipt" type="ReceiptXsltExtension, app_code"></add>
            <add name="customextensions" type="mycustomextensions.CustomExtensions" namespace="urn:CustomExtensions" />
        </extensions>
    </xsltobjects>
    entity.grid.xml.config (new code is red code)
    Code:
    <PackageTransform>
    
        <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                        xmlns:aspdnsf="urn:aspdnsf" 
                        xmlns:CustomExtensions="urn:CustomExtensions" 
                        exclude-result-prefixes="aspdnsf CustomExtensions">
    Code:
    <xsl:if test="aspdnsf:AppConfigBool('RatingsEnabled') = 'true'">
      <tr>
        <td height="45" align="center" valign="top">
          <xsl:value-of select="CustomExtensions:EntityPageRatings(ProductID)" disable-output-escaping="yes" />
        </td>
      </tr>
    </xsl:if>
    <a href="http://www.aspdotnetstorefront.com">Shopping Cart Software</a>