I want to add a review section for my products. i followed the wix tutorial . everything expect the product details is added to the database.
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
});
});
when i run the code its showing product id is undefined. please help me to solve this problem
It just means that the field in the dataset is hidden by default.
You can view it simply by going to your dataset, clicking on manage fields and you will see the field list and the shown/hidden switch where you can toggle the field to either shown or hidden.
Understanding the Code
Line 3: When the page loads, use the getContext function to get the object passed to the lightbox when it was opened. In this case, the object containins the current product ID.
Line 5: Set the action that occurs before the new review is saved to the reviews collection via the SubmitReviews dataset.
Line 6: Check if the reviewer rated the product.
Lines 7-8: If the reviewer did not rate the product, show the rateError error message and do not save the the new review.
Line 11: If the reviewer did rate the product, use the setFieldValues function to update the SubmitReviews dataset.
Lines 12-14: Update the dataset item with the input element values. The item will then be saved to the reviews collection.
Also, note that it might not be the lightbo that is causing the error as code for the product ID is used elsewhere as well.
Step 11: Prepare the Review Box Lightbox
In the Review Box lightbox, we started by importing the modules we need to work with data collections and lightboxes in our code. **Then we declare a variable to store the current product ID. **
When the lightbox finishes loading, we get the current product ID passed to the lightbox inStep 9 via the openLightbox function.
Step 9: Create the addReview_click Function on the Product Page
addReview_click runs when the reviewer clicks the “Write a Review” button. We selected the “Write a Review” button and, in the Properties panel, added an onClickevent handler .
The function opens the Review Box lightbox, sends it the current product’s ID, and waits for the lightbox to close.
export async function addReview_click(event, $w) {
const dataForLightbox = {
productId: product._id
};
let result = await wixWindow.openLightbox('Review Box', dataForLightbox);
$w('#Reviews').refresh();
loadStatistics();
$w('#thankYouMessage').show();
}
Understanding the Code
Lines 2-3: Create a dataForLightbox object contaning the current product’s ID to be sent to the Review Box lightbox.
Line 5: Open the Review Box lightbox, send it the product ID object created above, and wait for it to close.
Hello all, I might be out of your subject, but anyone knows what a dynamic parameter will be for product ID in Wix? I’m trying to get an answer from Wix devs for few months and nobody seems to know.
Any clue will be much appreciated.
Many thanks!
Please add your own issue into a new post instead of bumping up an old post - you will be more likely to get assistance if everyone can see your issue.
Explain what you are trying to do and why, what works, and what doesn’t. Also, add any code in a code block as stated in the Forum Guidelines .