Implementing rating on products and pages

In addition to a comment section, a rating can be added to your products and pages. Ratings can be displayed various places (Figure 1.1), which you will be guided through in this how to. Since the rating is a label fitting into the comment section, you might want to implement comments before implementing ratings.

How do I do that?

Figure 1.1 A product list with both ratings and comment counters displayed

The template you will be using is called Comment.cshtml and is located in Files > Templates > Comments.

This template contains:

  • A number of variables used in the markup
  • Some CSS styling which may be removed or modified as you see fit
  • A section for rendering comments which have been posted
  • Javascript for validating comments being posted
  • A section for rendering a comment box and posting a comment

We will be adding a new label to the comment box section.

Info: Remember to change the level comment params for the comment template (in the “writecomment” section) if you’re switching between comments on products and comments on pages.

To implement a rating option to your comment box, you must:

  • Open the Files node
  • Go to Templates > Comments and edit the Comment.cshtml file
  • Scroll all the way down to the bottom (in the “comment submission” part), and paste the following code amongst the other labels

 The section we are inserting could look like this:

RAZOR
<label for="Comment.Rating">Your rating</label> <select name="Comment.Rating" id="Comment.Rating"> <option value="">Rate product....</option> <option value="1">1 (Poor)</option> <option value="2">2 (Below average)</option> <option value="3">3 (Average)</option> <option value="4">4 (Good)</option> <option value="5">5 (Great)</option> </select><br />

You can change the options and their value at any time.

Which will look like Figure 3.2 in frontend:

Figure 3.2 A frontend example of a comment submitting form with rating

An entire “comment box” section of the Comment.cshtml template with a rating label could look like this. This template is to be used for ecom products only (change the level params in the template to enable it for pages).

Erase everything after “</script>” and paste the following instead:

RAZOR
<a name="writecomment"></a> <p> </p> <div class="heading"><h3 id="leavecomment">Comments regarding the product?</h3></div> <small id="inReplyTo"></small> <form method="post" action="/Default.aspx?ID=@GetGlobalValue("Global:Page.ID")" id="commentform" onsubmit="return comment_validate()"> <input type="hidden" name="Comment.Command" id="Comment.Command" value="" /> <!--Set active to false if you want to approve comments before publishing on the page--> <input type="hidden" name="Comment.Active" value="true" /> <!-- page level comments params --> @*<input type="hidden" name="Comment.ItemType" value="page" /> <input type="hidden" name="Comment.ItemID" value="@dwPageId" />*@ <!-- end of page level comments params --> <!-- product catalog level comments params --> <input type="hidden" name="Comment.ItemType" value="ecomProduct" /> <input type="hidden" name="Comment.ItemID" value="@productId" /> <input type="hidden" name="Comment.LangID" value="@langId" /> <!-- end product catalog level comments params --> <input type="hidden" name="Comment.Continue" value="@pageViewUrl" /> <input type="hidden" name="Comment.ParentID" id="Comment.ParentID" value="0" /> <input type="hidden" name="Comment.Notify" value="true" /> <input type="hidden" name="Comment.NotifyTemplate" value="Comments/Notify.html" /> <input type="hidden" name="Comment.NotifySubject" id="Comment.NotifySubject" value="New Comment on B&B: " /> <input type="hidden" name="Comment.NotifySenderEmail" value="noreply@dynamicweb.dk" /> <input type="hidden" name="Comment.NotifySenderName" value="Website comment" /> <input type="hidden" name="Comment.NotifyEmail" value="noreply@dynamicweb.dk" /> <input type="hidden" name="Comment.Reply.Notify" value="true" /> <input type="hidden" name="Comment.Reply.NotifyTemplate" value="Comments/ReplyNotify.html" /> <input type="hidden" name="Comment.Reply.NotifySubject" value="@hiddenSubject" /> <input type="hidden" name="Comment.Reply.NotifySenderEmail" value="noreply@dynamicweb.dk" /> <input type="hidden" name="Comment.Reply.NotifySenderName" value="Webmaster" /> <label for="Comment.Rating">Your rating</label> <select name="Comment.Rating" id="Comment.Rating"> <option value="">Rate product....</option> <option value="1">1 (Poor)</option> <option value="2">2 (Below average)</option> <option value="3">3 (Average)</option> <option value="4">4 (Good)</option> <option value="5">5 (Great)</option> </select><br> <label for="Comment.Name">Name <small>(Required)</small></label> <input type="text" name="Comment.Name" id="Comment.Name" value="@accessUserName" /><br> <label for="Comment.Email">E-mail <small>(Required, will not be published)</small></label> <input type="text" name="Comment.Email" id="Comment.Email" value="@accessUserEmail" /><br> <label for="Comment.Website">Website</label> <input type="text" name="Comment.Website" id="Comment.Website" value="" /><br> <label for="Comment.Text">Comment <small>(Required)</small></label> <textarea name="Comment.Text" id="Comment.Text" rows="10" cols="50">

You can display a products rating by the use of numbers, percent and stars. This can be shown anywhere on the product details page and on a regular or item-based page like a news article.

In Figure 4.1, the rating is shown in two different ways on the same product details page. See the template tag manual for more tags you can use for pages.

  • At the top, the rating is shown as a generic average value of the ratings given in the comments. This tag can be placed wherever you’d like in the ProductDetails.cshtml or ProductDetailsComment.cshtml template.
  • In the comment section, stars are used to display every comment’s rating of the product. This tag has to be placed inside the comment loop in the Comment.cshtml template.
    Comment sections are different than pages. Here you can only use these to tags:
    • Rating – will return the rating in numbers
    • Stars – will return the rating shown with five stars to be filled
Figure 4.1 Frontend example of the rating system in comments

You can display each product’s average rating in the product list (Figure 5.1). To do this, you must insert your tag inside the product loop of the Product list template (ProductList.cshtml).

Figure 5.1 Frontend example of a product list with average ratings

Info: There is already a product list template with rating and comparison implemented for the product catalog which is called ProductListCompareAndRating.html

To display the rating e.g. with stars in your product catalog, you must:

  • Go to your store and open the product catalog app
  • Open app settings
  • Under Templates, edit the List template
  • Inside the product loop, insert the following somewhere
RAZOR
<div class="rating comments" style="float: right;"> <span style="padding: 2px;">@product.GetString("Ecom:Product.RatingStars")</span> </div>

Since ratings are linked to their specific comment, you can moderate ratings by moderating the comments on your product/page.

To administer the comments for a product, you must go to the Ecommerce node and find the specific product and click Comments in the ribbon bar (Figure 6.1).

Figure 6.1 Go to the product to administer comments on a product

To administer the comment for a page, you must open the page and go to Properties. Open the Layout tab and click Comments in the ribbon bar (Figure 6.2).

Figure 6.2 Go to Properties to administer comments on pages

Both administer pages for comments will look like this (Figure 6.3). Here you can add, delete and change existing comments’ content, rating and other information.

Figure 6.3 The page for administering comments