Adding three related products

Hi,

I’m a beginner in Wix Code and I was wondering how I add more than 1 related products.
I checked the tutorials and my related products are working, but I only have one of them and I would love to have three related products.

Can someone tell me what I have to add?
This is the product part in my Javascript at the productpage right now:


// Find related products in the relationship collection by querying for related products in both relation directions.
let relatedByTable = await Promise.all([
wixData.query(‘related-products’)
.eq(‘productA’, productId)
.include(‘productB’)
.find(),
wixData.query(‘related-products’)
.eq(‘productB’, productId)
.include(‘productA’)
.find()
]);

// Merge related products found from both sides of the relationship collection.
let relatedProducts = [
…relatedByTable[0].items.map(_ => .productB),
…relatedByTable[1].items.map(
=> _.productA)
];


Thank you.

Hi Hellen,

In this article at step 6, you can see an explanation on how to create the showRelatedProducts() Function, so the repeater will present four products (you can change the amount of products according to your needs).

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();  
} 
} 

Moreover, this is a video that demonstrate how to add related products to a wix stores product page.

Have a nice day and best of luck,
Sapir,

Hi Sapir,
Thanks for your comment.
I was wondering what (and how) I have to change in this part…
Where do I add the products C-D-E?


Thanks a lot,
Hellen

Hi Hellen,

In this part you have nothing to change, because you are just creating an object that include all the relevant items to your product.

Can you please share a link to your site so we can inspect ?

Thanks,
Sapir

Hi Sapir,

Thanks for your quick respons.

It’s a private page so I can’t show you.
But I do have some screenshots;

Hi Hellen,

First, I suggest you to create your database with only two fields. You can write at Product A field the same product reference as you need and fetch to each item, the suitable product reference at Product B field.
It’s a better way to characterize your DB schema, because it prevents problems with empty fields, if a product is related to only 1-3 products and not 4( b-c-d-e).

In addition, if you wish to stay with your current database, view at the code below:

 let relatedByTable = await Promise.all ([
                wixData.query('related-products')
                .eq('productA', productId)
                .include('productB')
                .include('productC')
                .include('productD')
                .include('productE')
                .find()
            ]);

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

Have a nice day and best of luck!
Sapir,

Hi Sapir,

I applied the code you’d send me above.
It works in the ‘example page’ but not in the publiced page.
In the example page in the Wix editor I find my related products as created in the database.
In the publiced page the related products are radom…

Do you know what’s the problem?

And can you explain this a bit better, I’m a beginner in the coding:
“First, I suggest you to create your database with only two fields. You can write at Product A field the same product reference as you need and fetch to each item, the suitable product reference at Product B field.
It’s a better way to characterize your DB schema, because it prevents problems with empty fields, if a product is related to only 1-3 products and not 4( b-c-d-e).”

Thanks a lot!

Hi Hellen,

First, I believe that the problem its relate to the database’s permissions or you didn’t sync your data.
Here you can read more about collection permissions.
Here you can read more about sync data between sandbox and live collections.

Second, you created five fields at your database, at Product A field you write all your products and then for each item at Product A , you match four relate products references.(At Product B, Product C, Product D and Product E).
Instead of creating those fields, you can write the same product reference four times at product A field and then at Product B field match four relate products references.
It’s more correctly, because if one of the products at product A field will have lass or more then four relate products you won’t have to change the scheme of your database (add more fields or dealing with empty ones).


For more articles reference to wix code view the link below:

Best
Sapir,