Setting up reviews help please!

So i have followed the tutorial:

i have opened the editor and copied everything over to my site exactly.
However i get an error that it cant fetch the info i need from my product page to carry over to the review form.
I have checked my product databse and the products arent saved as ‘productId’ they are ‘inventoryitems’. i think this may be the issue?

Here is my code for the product page:

//-------------Imports-------------//

import wixData from ‘wix-data’ ;
import wixWindow from ‘wix-window’ ;

//-------------Global Variables-------------//

// Current product.
let product;

//-------------Page Setup-------------//

$w.onReady( async function () {
// Set the global product variable to the currently displayed product.
product = await $w( ‘#productPage1’ ).getProduct();
// Load the current product’s reviews using the initReviews() function.
initReviews();
});

// Loads the current product’s reviews.
async function initReviews() {
// Filter the “Reviews” dataset to contain only the reviews on the currently displayed product.
await $w( ‘#Reviews’ ).setFilter(wixData.filter().eq( ‘inventoryItem’ , product._ID));
// Show the reviews after the filter was set and applied on the dataset
showReviews();
// Load the current product’s statistics using the loadStatistics() function.
loadStatistics();
}

// Load the current product’s statistics.
async function loadStatistics() {
// Get the statistics data based on the current product’s ID.
const stats = await wixData.get( ‘review-stats’ , product._id);
// If statistics data for the product was found:
if (stats) {
// Compute the product’s average rating by dividing the total points by the number of ratings.
let avgRating = (Math.round(stats.rating * 10 / stats.count) / 10 );
// Compute the percentage of reviewers that recommend the product.
let percentRecommended = Math.round(stats.recommended / stats.count * 100 );
// Get the ratings element.
let ratings = $w( ‘#generalRatings’ );
// Set the ratings element’s average rating to the value calculated above.
ratings.rating = avgRating;
// Set the ratings element’s number of ratings to the count value from the statistics data.
ratings.numRatings = stats.count;
// Set the text for the recommended percentage element.
$w( ‘#recoPercent’ ).text = ${percentRecommended} % would recommend;
// Show the ratings element.
$w( ‘#generalRatings’ ).show();
// If there is no statistics data for the product:
} else {
// Set the text for the recommended percentage element to reflect the fact that there are no reviews.
$w( ‘#recoPercent’ ).text = ‘There are no reviews yet’ ;
}
// Show the recommended percentage element only after it is populated to avoid flickering.
$w( ‘#recoPercent’ ).show();
}

//-------------Repeater Setup -------------//

// Set up each item in the reivews repeater as it is loaded.
export function reviewsRepeater_itemReady($w, itemData, index) {
// If the reviewer recommends the item:
if (itemData.recommends) {
// Set the "recommend text.
$w( ‘#recommendation’ ).text = ‘I recommend this product.’ ;
// If the reviewer does not recommend the item:
} else {
// Set the “don’t recomend” text.
$w( ‘#recommendation’ ).text = “I don’t recommend this product.” ;
}

// If a photo was uploaded for the review:
if (itemData.photo) {
// Set the image URL from the item data.
$w( ‘#reviewImage’ ).src = itemData.photo;
// Expand the image.
$w( ‘#reviewImage’ ).expand();
}

// Set the ratings element’s rating value.
$w( ‘#oneRating’ ).rating = itemData.rating;

// Get the date that the review was entered.
let date = itemData._createdDate;
// Format the date according to the date format settings on the user’s computer.
$w( ‘#submissionTime’ ).text = date.toLocaleString();
}

//-------------Data Setup -------------//

// Perform some setup when the dataset filter was completed.
export function showReviews() {
// If at least one review has been submitted:
if ($w( ‘#Reviews’ ).getTotalCount() > 0 ) {
// Expand the strip that displays the reviews.
$w( ‘#reviewsStrip’ ).expand();
// If there are no reviews:
} else {
// Collapse the strip that displays the reviews.
$w( ‘#reviewsStrip’ ).collapse(); //otherwise, hide it
}
}

//-------------Event Handlers -------------//

// Set the action that occurs when a user clicks the “Write a Review” button.
export async function addReview_click(event, $w) {
// Create an object containing the current product’s ID to be sent to the review writing lightbox.
const dataForLightbox = {
inventoryItem: product._ID
};
// Open the “Review Box” lightbox, send it the object created above, and wait for it to close.
let result = await wixWindow.openLightbox( ‘Review Box’ , dataForLightbox);
// After the review lightbox is closed, refresh the reviews dataset so the new review appears on the page.
$w( ‘#Reviews’ ).refresh();
// Reload the current products statistics to reflect the new rating.
loadStatistics();
// Show a thank you message.
$w( ‘#thankYouMessage’ ).show();
}

// Set the action that occurs when a user clicks the “Load More” text.
export function resultsPages_click(event, $w) {
// Load additional reviews into the reviews repeater.
$w( ‘#Reviews’ ).loadMore();
}

Here is my lightbox Code:

//-------------Imports-------------//

import wixWindow from ‘wix-window’ ;
import wixData from ‘wix-data’ ;

//-------------Global Variables-------------//

// Current product’s ID.
let productId;

//-------------Lightbox Setup-------------//

$w.onReady( function () {
// Get the data passed by the page that opened the lightbox.
productId = wixWindow.lightbox.getContext().productId;

// Set the action that occurs before the review is saved.
$w( ‘#SubmitReviews’ ).onBeforeSave(() => {
// If no rating was set:
if ($w( ‘#radioRating’ ).value === ‘’ ) {
// Display an error message.
$w( ‘#rateError’ ).show();
// Force the save to fail.
return Promise.reject();
}

// If a rating was set, set the element values into the fields of the dataset item.
// These values will be saved in the collection.
$w( ‘#SubmitReviews’ ).setFieldValues({
productId,
rating: $w( ‘#radioRating’ ).value,
recommends: $w( ‘#radioGroup1’ ).value
});
});

// Set the action that occurs after the review is saved.
$w( ‘#SubmitReviews’ ).onAfterSave( async () => {
// Update the product’s statistics using the updateStatistics() function.
await updateStatistics($w( ‘#radioGroup1’ ).value);
// When the statistics have been updated, close the lightbox to return the user to the product page.
wixWindow.lightbox.close();
});
});

// Update (or create) the product statistics.
async function updateStatistics(isRecommended) {
// Get the review statistics for the current product from the “review-stats” collection.
let stats = await wixData.get( ‘review-stats’ , productId);

// If statistics data already exist for this product:
if (stats) {
// Add the new rating to the total rating points.
stats.rating += parseInt($w( ‘#radioRating’ ).value, 10 );
// Increase the ratings count by 1.
stats.count += 1 ;
// Increase the recommendations by one if the user recommends the product.
stats.recommended += (isRecommended === “true” ) ? 1 : 0 ;
// Update the new product statistics in the “review-stats” collection.
return wixData.update( ‘review-stats’ , stats)
}
//If no statistics data exists for this product, create a new statistics item.
stats = {
// Set the statistics item’s ID to the current product’s ID.
_id: productId,
// Set the statistics item’s rating to the rating entered by the user.
rating: parseInt($w( ‘#radioRating’ ).value, 10 ),
// Set the statistics item’s ratings count to 1 because this is the first rating.
count: 1 ,
// Set the statistics item’s recommended property to 1 if the user recommends the product.
recommended: (isRecommended === “true” ) ? 1 : 0
};
// Insert the new product statistics item into the “review-stats” collection.
return wixData.insert( ‘review-stats’ , stats)
}

//-------------Event Handlers-------------//

// Set the action that occurs when a rating is chosen.
export function radioRating_change(event, $w) {
// Hide the error message.
$w( ‘#rateError’ ).hide();
}

Here is the error i am getting:

I just wish to get it working then im thinking of re-doing my store using repeaters then i can link to a star rating under each product somehow??

Or somebody do it for me and i will pay you :):):):):slight_smile:

Thanks
Matt

You wrote " copied everything over to my site exactly". From where did you copy? Was this from a tutorial or an example? Is so, which one? And did you try running the original?

I opened the example site at the top of this post and had it side by side to my website, I copied the strip and boxes etc and pasted them into mine.

I then copied all the code and pasted into mine.
I then made my own databases and called everything the same as in the example site off the tutorial.

It all seemed ok but something was missing, it doesn’t even open the review form when i click ‘write review’. I think that’s where the problem is arising somewhere around that part of the code.

But like I said its all copied exactly and the example site works.
Something must be amiss.

I have done it 3 times now and the same error occurs each time.
I have deleted everything and re done it all to make sure no mistakes were made.

Its as if it cant get the product info from the product page. I may be wrong though as i am no expert.

Any help is much appreciated. I just want a basic product review on my store for each product, then to show the % of people that recommend that product. Then if you press show reviews for it to expand a strip and show said reviews.

Thanks for your reply.

Matt

@mhall0405 Do you have products in your Store?

@yisrael-wix yes i do, i have an up and running store and i have just re done it all now and its still not working,
the reviews are showing when i enter them manually on the databse but when i preview and then try and press ‘write review’ nothing happens.
the error i had before has gone off that line now but still nothing happen at the ‘onClick’ event

@mhall0405 What is the URL of your site, and on what page? How can I see the problem?