Skip to content

Instantly share code, notes, and snippets.

@nucab
Forked from elmogallen/BVProductList.html
Created January 1, 2016 22:29
Show Gist options
  • Save nucab/9319320d889b72ef3e54 to your computer and use it in GitHub Desktop.
Save nucab/9319320d889b72ef3e54 to your computer and use it in GitHub Desktop.
Inline Bazaarvoice Ratings and Reviews with Read Reviews link
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<!-- TODO:
Do some kind of loop here to show your results.
Inside the loop, you'd generate HTML similar to the following: -->
<div id="BVRRInlineRating-{productId}"></div>
<a href="" class="bvReadReviews" data-pid="{productId}">Read reviews</a>
<!-- End Loop That Generates HTML for results -->
<script type="text/javascript">
// TODO:
// You'll also have to fill the "pids" variable with all the productIds you're showing ratings/reviews for.
// The pids correspond to "ProductIds", which are actually called "ExternalIds" in the Product XML Data Feed.
var pids = "12345,23456,34567";
$(document).ready(function() {
$BV.ui('rr', 'inline_ratings', {
productIds: pids,
containerPrefix: 'BVRRInlineRating'
});
$(".bvReadReviews").click(function () {
window.open('/BVReviewDetail.html?pid=' + $(this).attr("data-pid"), "reviewWindow", "toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=750,height=600");
return false;
});
});
</script>
<script type="text/javascript" src="//display-stg.ugc.bazaarvoice.com/static/{your implementation}/en_US/bvapi.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div id="BVRRContainer"></div>
<script type="text/javascript">
// returns the value of the specified querystring parameter
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
// User would go to BazaarvoiceExample.html?pid=123
// where 123 is the ExternalId of a product in the XML data feed
$(document).ready(function() {
var pid = getParameterByName("pid");
$BV.ui('rr', 'show_reviews', {
productId: pid
});
});
</script>
<script type="text/javascript" src="//display-stg.ugc.bazaarvoice.com/static/{your implementation}/en_US/bvapi.js"></script>
// productIds is an array of strings
for (var i=0; i < productIds.length; i++) {
(function () {
var productId = productIds[i];
var url = "http://stg.api.bazaarvoice.com/data/reviews.json?apiversion=5.4&passkey={secretkey}&Filter=ProductId:" + productId + "&Filter=IsRatingsOnly:false&Limit=1";
$.get(url, function( data ) {
var totalReviews = data.TotalResults || 0;
$("#rrLink" + productId).text("Read " + totalReviews + " reviews");
})
.fail(function( jqXHR, textStatus, errorThrown) {
console.log( textStatus + " " + errorThrown );
});
}());
}
// alternative method which wouldn't require an id on the anchor tag:
$("a.bvReadReviews").each(function (index, element) {
link = $(element);
var url = "http://stg.api.bazaarvoice.com/data/reviews.json?apiversion=5.4&passkey={secret key}&Filter=ProductId:" + link.attr("data-dealerid") + "&Filter=IsRatingsOnly:false&Limit=1";
$.get(url, function( data ) {
var totalReviews = data.TotalResults || 0;
link.text("Read " + totalReviews + " reviews");
})
.fail(function( jqXHR, textStatus, errorThrown) {
console.log( textStatus + " " + errorThrown );
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment