Blog

Tutorial: Amazon.com Search API

Posted on

8. Ratings, Reviews & Sellers

Ratings, Reviews & Sellers Code

// Ratings, Sellers & Reviews
      // Define Containers
      var subInfo = cel('div');
      subInfo.setAttribute("id", "subInfo");
      var subInfoUl = cel('ul');
      var subInfoLi1 = cel('li');
      var subInfoLi2 = cel('li');
      var subInfoLi3 = cel('li');
      var subInfoA1 = cel('a');
      var subInfoA2 = cel('a');
      var subInfoA3 = cel('a');
      var subInfoIg = cel('img');
      var selliconIg = cel('img');

      // Ratings
      subInfoIg.setAttribute('src', rateimage(tmpItem.avgrating));
      subInfoIg.setAttribute('alt', tmpItem.avgrating + ' Star Average');
      subInfoIg.setAttribute('align', 'center');
      subInfoIg.style.cssText = "border:0px;height:11px;width:60px;clear:none;margin-right: 5px;padding-right: 5px;";
      subInfoA1.style.cssText = "font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8px; color: #0068B3; font-weight: bold;";
      subInfoA1.setAttribute('href', tmpItem.url);
      subInfoA1.setAttribute('target', '_blank');
      subInfoA1.appendChild(subInfoIg);

      // Reviews
      subInfoA2.style.cssText = "font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8px; color: #0068B3; font-weight: bold;";
      subInfoA2.setAttribute('href', tmpItem.url);
      subInfoA2.setAttribute('target', '_blank');
      subInfoA2.appendChild(ctn(tmpItem.totalreviews + ' Reviews'));
      if (Number(tmpItem.totalreviews) > 0)
      {
         subInfoLi1.appendChild(subInfoA1);
         subInfoLi2.appendChild(subInfoA2);
      }
      else
      {
         subInfoLi1.style.cssText = "font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8px; color: #666666; font-weight: bold; ";
         subInfoLi1.appendChild(ctn('No Votes'));
         subInfoLi2.appendChild(subInfoA2);
      }

      // Sellers
      selliconIg.setAttribute('src', 'images/sa_seller_ico.gif');
      selliconIg.setAttribute('alt', tmpItem.totalsellers + ' Sellers');
      selliconIg.style.cssText = "border:0px;height:10px;width:11px;clear:none;margin-right: 5px;";
      subInfoA3.style.cssText = "font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8px; color: #666666; font-weight: bold;";
      subInfoA3.setAttribute('href', tmpItem.url);
      subInfoA3.setAttribute('target', '_blank');
      subInfoA3.appendChild(selliconIg);
      subInfoA3.appendChild(ctn(tmpItem.totalsellers + ' Sellers'));
      if (Number(tmpItem.totalsellers) > 0)
      {
         subInfoLi3.appendChild(subInfoA3);
      }
      else
      {
         subInfoLi3.style.cssText = "font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8px; color: #666666; font-weight: bold; ";
         subInfoLi3.appendChild(ctn('No Sellers'));
      }

      // Build Ratings, Reviews & Sellers Display
      subInfoUl.appendChild(subInfoLi1);
      subInfoUl.appendChild(subInfoLi2);
      subInfoUl.appendChild(subInfoLi3);
      subInfo.appendChild(subInfoUl);

RateImage Function Code

// Get Rating Image Source

function rateimage(score)
{
   var score2img = Array();
   var imgsrc = "";
   score2img['0'] = 'images/rating_0_0_newr.gif';
   score2img['0.5'] = 'images/rating_0_5_newr.gif';
   score2img['1'] = 'images/rating_1_newr.gif';
   score2img['1.5'] = 'images/rating_1_5_newr.gif';
   score2img['2'] = 'images/rating_2_newr.gif';
   score2img['2.5'] = 'images/rating_2_5_newr.gif';
   score2img['3'] = 'images/rating_3_newr.gif';
   score2img['3.5'] = 'images/rating_3_5_newr.gif';
   score2img['4'] = 'images/rating_4_newr.gif';
   score2img['4.5'] = 'images/rating_4_5_newr.gif';
   score2img['5'] = 'images/rating_5_newr.gif';

   score = ((Math.round(score * 2)) / 2);
   var imgsrc = score2img[score];

   return imgsrc;
}

The above code snippets address the display of the average rating, number of reviews and sellers. The graphic ratings display is a selection from a series of star images. The RateImage function rounds the average rating to the nearest 0.5 and assigns a corresponding image source url for the IMG element. All the anchors for this section point to the main item page on Amazon, however, a little additional research may uncover whether there is a separate url for a ratings, review and seller list.