Hi, I am trying to create a related products repeater on the products page that queries products that are in the same collection as the product as well as the same price range. My issue is that I am unable to pull the name or id of the collection that the product is a part of. This is because the ‘collections’ field is a multi-reference field. I have attached my code below.
async function relatedProductsByPrice(product) {
// Get the current product’s ID. let productId = product._id; let productCollection = product.collections._id; ******ISSUE IS HERE
If there is any way to pull the ‘name’ or ‘_id’ field for the collection then I can query easily. I am not sure if it is a syntax mistake or if I need to change my method.
You didn’t write how you got this product in the first place (how you passed it to the function. If you didn’t use .include() to get this product so obviously the reference field won’t be there.
Hi JD, so my code is quite long for the products page which is why I didn’t post it all. I have attached an example of my products page below where you can see that there are two repeaters. The first repeater shows all products where the name of the products starts with the same name as the main product. This repeater is working fine. The second repeater is the one I am struggling with. I am able to find the products that are within the same price range but I’m not able to show only products in the same collection (in this case “loveseats”).
The code below is the query function for the first repeater. This is working perfectly.
async function relatedProductsBySeries(product) {
// Get the current product’s ID.
let productId = product._id;
let productTitle = product.name;
let productSplit = productTitle.split( ’ ’ ).slice( 0 , 1 );
productTitle = productSplit[ 0 ];
// Query the “Products” collection for product’s whose price is within 20% of the current product’s price.
let relatedBySeries = await wixData.query( ‘Stores/Products’ )
.startsWith( ‘name’ , productTitle)
.ne( ‘_id’ , productId)
.find(); return relatedBySeries.items;
}
The code posted above is the code used in the second repeater. That is where Im having issues. Hopefully this clears it up. I have also attached my full code if you want to take a look.
Hi JD, I have the .include(‘collections’) in the query but the issue still remains that I don’t have a way of checking which collection the product is in.
async function relatedProductsByPrice(product) {
// Get the current product’s ID.
let productId = product._id;
let productCollection = product.collections.id;
console.log( "id: " + product.collections.id)
What do you suggest then? iv tried the same approach but putting product.collections.id directly in the query but that didnt work either.
async function relatedProductsByPrice(product) {
// Get the current product’s ID. let productId = product._id;
//let productCollection = product.collections.id; //console.log("id: " + product.collections)
I only see part of your code and I don;t really know what you’re trying to achieve, so I can’t make suggestions. But first of all you need to get the specific product with include() and only then you can use it.
In your last post to trued to query .hasSome(“id”, product.collections.id)) without having it first. Think about it. if you don’t have the product.collections.id before this query, how can you use it in the query?