I would be so grateful if someone can sort this out. Recently, I tried following the instructions for creating a Related Items section on my product page. The images load fine but when I click on the image of one of the related items, the link changes in the web address bar but the page does not refresh???
Even on the tutorial, I noticed it does not refresh there either. This seems to be a common problem with wixLocation.to(product.productPageUrl) and no one seems to have an answer?
Here’s my code -
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;
$w.onReady( function () {
loadRelatedProducts();
});
async function loadRelatedProducts() {
let product = await $w(‘#productPage’).getProduct();
let relatedProductResults = await Promise.all([
relatedProductsByTable(product),
relatedProductsByPrice(product)
]);
if (relatedProductResults[0].length > 0)
showRelatedProducts(relatedProductResults[0]);
else
showRelatedProducts(relatedProductResults[1]);
}
async function relatedProductsByTable(product) {
let productId = product._id;
// find related products by relation table
let relatedByTable = await Promise.all([
wixData.query(‘relatedProducts’)
.eq (‘productA’, productId)
.include(‘productB’)
.find(),
wixData.query(‘relatedProducts’)
.eq (‘productB’, productId)
.include(‘productA’)
.find(),
]);
let relatedProducts = [
…relatedByTable[0].items.map(_ => .productB),
…relatedByTable[1].items.map( => _.productA)
];
return relatedProducts;
}
async function relatedProductsByPrice(product) {
let productId = product._id;
// find related products by price
let relatedByPrice = await wixData.query(‘Stores/Products’)
.between(‘price’, product.price * 0.8, product.price * 1.2)
.ne(‘_id’, productId)
.find();
return relatedByPrice.items;
}
function showRelatedProducts(relatedProducts){
if (relatedProducts.length > 0){
relatedProducts.splice(4, relatedProducts.length);
$w(‘#relatedItemsRepeater’).onItemReady(relatedItemReady);
$w(“#relatedItemsRepeater”).data = relatedProducts;
$w(“#relatedItems”).expand();
}
else {
$w(“#relatedItems”).collapse();
}
}
function relatedItemReady($w, product){
$w(“#productImage”).src = product.mainMedia;
$w(“#productName”).text = product.name;
$w(“#productPrice”).text = product.formattedPrice;
$w(‘#productImage’).onClick( (event) => {
wixLocation.to(product.productPageUrl);
});
}