Hi,
I have made a custom product page and using repeater i want to show related products which are relevant to the product.
I tried with an article available in the wix code as an example but it is giving me error at a function name getProduct().
Please if anyone can help me out I’m stuck at it and trying to resolve since morning but not able to resolve it. Can anyone tell me where i’m wrong it is giving me error “getProduct() doesnot exist in the product page”
Except getProduct there is no error.
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(() => {
wixLocation.to(product.productPageUrl);
});
}