The current custom “reference product Store implementation” is defined with all reference product defined in ProductB column.
It become quite cumbersome for the admin to define reference when we have many reference product instead just few.
To make life easy and better user experience and reducing chance of error…I have defined reference products spreaded in dedicated columns>
Problem Stmt: The new declaration of defining reference product is working fine when all reference fields are populated , but the code errors if any left empty.
Pls help, in finetune the code which can handle if few had 5 references and few have only 2 references.
Sharing reference pics of our custom store implementation>
async function loadRelatedProducts() {
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;
let relatedByTable = await Promise.all([
wixData.query('relatedProducts')
.eq('productA', productId)
.include('productB')
.include('productC')
.include('productD')
.include('productE')
.find()
]);
//added 7 May
let relatedProducts = [
...relatedByTable[0].items.map(_ => _.productB),
...relatedByTable[0].items.map(_ => _.productC),
...relatedByTable[0].items.map(_ => _.productD),
...relatedByTable[0].items.map(_ => _.productE),
];
return relatedProducts;
}
function showRelatedProducts(relatedProducts) {
$w("#text37").text= relatedProducts.length.toString();
// if (relatedProducts.length > 0) { //commented 5 may
relatedProducts.splice(4, relatedProducts.length);
$w('#relatedItemsRepeater').onItemReady(relatedItemReady);
$w("#relatedItemsRepeater").data = relatedProducts;
}