steveosoule
11/5/2013 - 9:45 PM

Miva Merchant - Save PowerReviews Rating to Custom Field

Miva Merchant - Save PowerReviews Rating to Custom Field

// Save PowerReviews rating amount and count to custom product field
$(window).load(function(){
	var debug = false,
		rating,
		count,
		productId = $('#product-id').text(),
		timesChecked = 0,
		storedRating = $('[itemprop="ratingValue"]').text(),
		storedCount = $('[itemprop="reviewCount"]').text();

	function checkForRating(){
		rating = $('.pr-snippet-rating-decimal').text();
		count = $('.pr-snippet-link span').text();

		if(rating !== ''){
			validateRating();
		} else if (timesChecked < 100) {
			if (debug) console.log('checked'+$('.pr-snippet-rating-decimal').text()+'-'+$('.pr-snippet-link span').text());
			timesChecked += 1;
			setTimeout(function(){checkForRating()},250);
		}
	}
	checkForRating();

	function validateRating(){
		if(rating !== '0.0'){
			if(storedRating !== rating || storedCount !== count){
				saveRating();
			}
		}
	}

	function saveRating(){
		// ajax call to page that will save the rating and count
		$.ajax({
			url: '/mm5/merchant.mvc',
			type: 'POST',
			cache: false,
			data: {
				Screen: 'PROD',
				powerReviewsRating: rating,
				powerReviewsCount: count,
				updateProductId: productId
			}
		});
	}
});