Gift Quiz not producing product

I’ve tried all of the solutions given on other forums on this subject. Somebody please help!

Below is the database

Below is the example of what’s wrong

Below is the link to try it out yourself

https://sparkedcoffeeroast.wixsite.com/mysite/home

Below is my code used for the project


import wixData from 'wix-data';
const selectedAnswers = [];
const quizAnswersIds = ["11", "12", "13", "14", "15", "16", "21", "22", "23", "24", "25", "26" ];
let numberOfSlides;
$w.onReady(() => {
numberOfSlides = $w('#quizSlides').slides.length;
quizAnswersIds.forEach(answer => {
$w(`#${answer}`).onClick(() => {
selectedAnswers.push(answer);
$w('#quizSlides').next();
})
})
});
export async function quizSlides_change(event) {
//Add your code for this event here:
if (isQuizFinished()) {
let quizProducts = await getKeywordsPerProduct();
selectedAnswers.forEach(answer => {
quizProducts = filterProductsPerAnswer(quizProducts, answer);
});
quizProducts = quizProducts.map(quizProduct => quizProduct.productId);
quizProducts = getRandomItemsFromArray(quizProducts, numberOfSlides);
$w('#recommendedProducts').data = await getProductsData(quizProducts);
}
}
function isQuizFinished() {
return $w('#quizSlides').currentIndex === numberOfSlides - 1;
}
async function getKeywordsPerProduct() {
let productsKeywords = await wixData.query("productsKeywords").find();
productsKeywords = productsKeywords.items;
productsKeywords = productsKeywords.map(item => {
return {
productId: item.product,
keywords: item.keywords.split(",")
}
});
return productsKeywords;
}
function filterProductsPerAnswer(quizProducts, answer) {
const filteredProducts = quizProducts.filter(quizProduct => {
return quizProduct.keywords.includes(answer)
});
return filteredProducts;
}
function getRandomItemsFromArray(productsArr, numberOfItems) {
const productsIds = [];
let numberOfProducts = productsArr.length;
for (let i = 0; i < numberOfItems && i < numberOfProducts; i++) {
const randomIndex = getRandomInt(0, productsArr.length - 1);
productsIds.push(productsArr[randomIndex]);
productsArr.splice(randomIndex, 1);
}
return productsIds;
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
async function getProductsData(productsIds) {
const productsData = await wixData.query("Stores/Products")
.hasSome("_id", productsIds)
.find();
return productsData.items;
}
export function recommendedProducts_itemReady($item, itemData, index) {
//Add your code for this event here:
$item('#name').text = itemData.name;
$item('#image').src = itemData.mainMedia;
$item('#image').link = itemData.productPageUrl;
$item('#price').text = itemData.formattedPrice;
}

Hi,

We are aware that following this tutorial now does not result in showing the final product after the quiz and we are currently working on this matter and updating the tutorial.

Thank you for your patience and understanding.

Have a nice day!

Thanks for bringing this into our attention,
the tutorial is now fixed and should be good to go, if you are having any more difficulties regarding the example please let us know in this thread

Hi,
I am trying to set up a quiz, where I will receive only one product.
So in the in the productsKeywords I’ve assigned 27 combinations (3 questions with 3 different answers) of key words to match certain products.
How do I change the code to receive only one, matching product?
Please help :slight_smile:

Hi @maciek687 ! I am also trying to achieve the same, limit the results to just one matching product. Did you ever figure this out?