Code to link Reviews to Sellers

Question:
Hello! I am creating a no-code website with 2 types of members - sellers and buyers. I want buyers to leave reviews about sellers. I have added the review elements to the profiles of sellers; however, there is no link to the specific seller in the standard Wix functionality and all reviews show up for all sellers. I need code that will insert the ID of the seller to a reference field in my reviews collection and can be used afterwards to filter on a specific seller. Is it possible to add just a few lines of code to expand the standard Wix code? Could someone give me those lines please? Thank you.

Product:
Wix Studio Editor

What have you already tried:
I have added a reference field to my Reviews collection but don’t know how to populate it. Searched on the web and found just one video that explains how to create manually the reviews functionality. It is a complete solution with a lot of code. Since I don’t have much knowledge about coding, I am afraid I might break something on the website if I put in code to replace the whole pre-build functionality. I need something simple.

I added this code, where the IDs are those of (my Reviews database, multi reference field for Seller in the Reviews collection, review id, seller id). The button works as normal, not being affected by the code at all.
$w(‘#button4’).onClick((event) => {
wixData.insertReference(“q0a0bouo6cw0a8aamsxc”,“multireference”,“_id”,“_id”)
})

Hello,

Absolutely! You’re on the right track with adding a reference field to your Reviews collection. We can definitely expand the standard Wix review functionality with a few lines of code to link each review to the specific seller.

Here’s how we’ll approach it:

Identify the Seller’s ID: We need to retrieve the unique ID of the seller whose profile is being viewed.
Populate the Reference Field: We’ll use Wix Data’s save() function to update the Reviews collection with the seller’s ID in the reference field.
Here’s the code you can add to your seller profile page (where the reviews are displayed):

JavaScript

import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;

$w.onReady(function () {
// Function to save a review with the seller’s ID
$w(“#reviewSubmitButton”).onClick(() => { // Replace “reviewSubmitButton” with the actual ID of your review submit button
const sellerId = wixLocation.path[1]; // Assuming the seller’s ID is the second segment in the URL (e.g., /sellers/sellerID)

if (sellerId) {
  // Get the review data from the input fields
  let reviewData = {
    title: $w("#reviewTitleInput").value, // Replace "reviewTitleInput" with the actual ID
    content: $w("#reviewContentInput").value, // Replace "reviewContentInput" with the actual ID
    rating: $w("#ratingInput").value, // Replace "ratingInput" with the actual ID
    seller: sellerId, // Add the seller ID to the review data
  };

  // Save the review to the "Reviews" collection
  wixData.insert("Reviews", reviewData)
    .then(() => {
      console.log("Review saved successfully with seller ID:", sellerId);
      // Optionally, refresh the reviews display
      $w("#reviewsDataset").refresh(); // Replace "reviewsDataset" with the actual ID of your reviews dataset
    })
    .catch((err) => {
      console.error("Error saving review:", err);
    });
} else {
  console.error("Seller ID not found in URL.");
}

});
});
Important Notes:

Replace Placeholders:
“reviewSubmitButton”: Replace this with the actual ID of the button that submits the review.
“reviewTitleInput”, “reviewContentInput”, “ratingInput”: Replace these with the actual IDs of your review input fields.
“reviewsDataset”: Replace this with the actual ID of the dataset that displays your reviews.
wixLocation.path[1] : this is assuming that your seller profile url is formated like this: /sellers/sellerID. if your url is different, you will need to change the number inside the square brackets to reflect the correct segment of the url.
URL Structure: This code assumes that the seller’s ID is the second segment in the URL (e.g., /sellers/sellerID). If your URL structure is different, you’ll need to adjust the wixLocation.path[1] part accordingly.
Reference Field: Make sure that the “seller” field in your reviewData object matches the name of your reference field in the “Reviews” collection.
Dataset Refresh: The $w(“#reviewsDataset”).refresh(); line is optional. It refreshes the reviews display after a new review is submitted. If you’re using a repeater or other dynamic element to display reviews, you might need to adjust this part.
Error Handling: The catch() block handles potential errors during the wixData.insert() operation. It’s good practice to include error handling to troubleshoot issues.

Best Regards

Thank you, @user5380 for the code and the elaborated explanation! :slight_smile:

In the meantime, I was playing with the form, filters, permissions… and somehow it worked when I added a dropdown field to the Review input form. I made the field to default to the name of the Seller and then submit it to the Reviews collection together with the rest of the data.

But now I have another issue - the display of ratings. The total score and total number of reviews do not calculate correctly after I added the filter for Seller. Before the filter, they were showing the totals for all Sellers and those totals were correct. After the filter, the display of the reviews works well and shows correct information. However, the total score and total number of reviews are some very inaccurate numbers. Any idea why? Adding a screenshot.

The weird thing is that I use exactly the same dataset for the Ratings display with the same connections.

The filter is looking for the name of the seller submitted with the Review form into another collection with Seller data: (NameSeller) is (Name from the other collection)