But so is Inventory Item is a reference field and it doesn’t return undefined.
For example, if check the Wix Blog app. To get the categories for the current post (The categories is also a multi reference field) I do not get “undefined”. It returns the actually array of categories in that field.
But when I do this for Wix Stores, to get what categories (or “collections”) array the current product has in that field, it always returns “undefined”. As if the field was blank. Which it’s not. So how do I return the categories the product belongs to?
You definitely can pull it. I haven’t used getProduct(), but querying the Stores/Products ( with its custom limit on 100 items) database directly does give both the products and the collection IDs, which you can cross reference against the Stores/Collections Database. I’m guessing you tried that already?
Actually Quentin jumped in video call with me to brainstorm.
Results …
Cannot get collections info via getProduct().
Cannot get collections info via dataset filter.
Cannot get collections info via dataset connections.
CAN get collections info via query ONLY.
You can use query + include - if you need this for multiple products
wixData.query( “Stores/Products” ).include( “collections” ).find().then(x => {
console.log(x.items[ 0 ].collections);
});
You can query referenced - if you need this for a single product
wixData.queryReferenced( “Stores/Products” , “f2c944b4-9766-913c-e0e2-41a6e0136989” , “collections” ).then(x => {
console.log(x.items);
});
$w(“#productPage).getProduct does not support returning the collections at all as you saw in the documentation. I agree that the fact that we return undefined (both here and when include is missing) is confusing.
I can also imagine another parameter sent to $w(”#productPage).getProduct which will allow you to ask for collections, but I wouldn’t say it has a huge value since you can use query referenced.
@chris-derrell For sure. Sad that they did not include it in the getProducts() API for Wix Stores but they did include for the getPost() API in Wix Blog. Inconsistencies like that make it more challenging to remember which app does or does not ‘have this or that’.
It would be of great value to keep the API’s consistent. We are able to pull such information from the Wix Blog API (getPost) but not from Wix Stores API (getProduct).
Every other field for that product can be retrieved by using getProduct().
Now we have to build an entirely separate query just to retrieve the 1 last field from the Stores/Products collection because getProduct won’t let us view that single field value. It is not very efficient.
@code-queen
While I understand the need for consistency, I will explain the reasoning behind this.
A product, can be part of many collections. Could 3, 10, maybe 1000 or more.
Such use cases, require another API to allow pagination over the referenced item, in this case collections.
So while I understand that making another API call adds complexity, I’m afraid that it’s a must in this case
Please let me know if that makes sense to you
Note that I did not address the question of why blog API works the way it does, simply because I do not know the specifics of that product