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)
];
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).
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:
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).”
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).