Issue with related products

Hello,

I have prepared related products on my page www.metallzaun.de and it worked fine.

However since a couple of days, when you click a related product it goes up to the picture but i does not change the product. Please check it.

This is very annoying and being honest we already want to resign from using wix. This solution for related products is very unprepared. Sometimes when you click ctr F5 the pictures are not added properly. In general this is really bad.

Best regards

This forum is dedicated to Wix Code. For questions that are not related to code you can contact the Wix support team . You’ll get better help for your problem there.

If you feel that this issue is related to code that you’ve added to your site, then please post more details.

Dear Yisrael,

this is dedicated to code, I checked posts on this forum how to prepare the code.

Please check your related product sample page. It does not work also. At least for me and my firends who checked that. Something is wrong how the code works.

Best regards

OK, we’ll check this out.

It seems that there was a database issue with the template for this example. It has now been fixed. Load the (new) template into your Editor and use that as your guide.

Dear Yisrael,

but what do you mean with template? I have prepared the code myself, same as this forum says. If I load a new template into my editor I will lose what I changed in template? Still does not work

This is the 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([
relatedProductsByCollection(product),
]);

if (relatedProductResults[0].length > 0)
// Show the related products from the collection.
showRelatedProducts(relatedProductResults[0]);
}

async function relatedProductsByCollection(product) {

let productId = product._id;

let relatedByTable = await Promise.all([
wixData.query(‘relatedProducts’)
.eq(‘productA’, productId)
.include(‘productB’)
.find(),
]);

let relatedProducts = [
…relatedByTable[0].items.map(_ => _.productB),
];

return relatedProducts;
}

function showRelatedProducts(relatedProducts){

if (relatedProducts.length > 0){

    relatedProducts.splice(4, relatedProducts.length); 

    $w('#relatedItemsRepeater').onItemReady(relatedItemReady); 

    $w("#relatedItemsRepeater").data = relatedProducts; 

    $w("#relatedItems").expand(); 
} 

// If there are no related products:
else {
// Collapse the related items repeater.
$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(() => { 
    wixLocation.to(product.productPageUrl); 
}); 

}

Do you mind please explain what has been changed in the code? As I use the old template and modified for need, I will need to know what caused the problem to modify my own code.

@yisrael-wix Do you mind please explain what has been changed in the code? As I use the old template and modified for need, I will need to know what caused the problem to modify my own code.
Also, have just checked your new template, and the sample site is still not functioning properly

@inituhome You are right, even the template it does not work

hello!!!
try this,
change the function and add the url of your site to “yoursiteurl”
function relatedItemReady($w, product){
$w(" #productImage “).src = product.mainMedia;
$w(” #productName “).text = product.name ;
$w(” #productPrice “).text = product.formattedPrice;
$w(’ #productImage ').onClick(() => {
wixLocation.to (” https://yoursiteurl/product-page/ " + product.slug);
});
}