# Code example: get product collection for a Wix Stores product

**URL:** <https://forum.wixstudio.com/t/code-example-get-product-collection-for-a-wix-stores-product/27093>\
**Category:** Ask the community\
**Tags:** wix-stores, code, question\
**Created:** [April 11, 2020, 3:41pm UTC](https://forum.wixstudio.com/t/code-example-get-product-collection-for-a-wix-stores-product/27093 "2020-04-11T15:41:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![verbaldancing](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/verbaldancing/32/10074_2.png) [@verbaldancing](https://forum.wixstudio.com/u/verbaldancing)\
**Post date:** [April 11, 2020, 3:41pm UTC](https://forum.wixstudio.com/t/code-example-get-product-collection-for-a-wix-stores-product/27093/1 "2020-04-11T15:41:01Z")

</div>

OK I spent hours figuring this out, it is very tedious because you can’t get it from Datasets in the editor. Yes, I am talking about getting the product collection for a product in Wix Stores. 🙂 Just sharing it here so you don’t have to solve the same puzzle

This code gets one collection name for each product, filtering out an unnecessary collection name (‘retail’ in this example).

Create a backend module called shop.jsw

import wixData **from** ‘wix-data’;  
**let** options = {  
“suppressAuth”: **true** ,  
“suppressHooks”: **true**  
};

export function getcollection(id) {  
**return** wixData.queryReferenced(“Stores/Products”, id, “collections”)  
.then((results) =\> {  
**if** (results.items.length \> 0) {  
**let** collections = results.items.map(x =\> x.name)  
**let** filteredcollections = collections.filter(item =\> item !== “Retail”)  
**return** filteredcollections[0]  
} **else** {  
console.log(“no collection found”)  
}  
})  
}

Frontend code: In this example, I get the product ID from the product page in Wix Stores. I get the collection name by calling the backend function above and I pass it on to an element on the page

import { getcollection } **from** ‘backend/shop’

$w.onReady( **function** () {  
$w(‘#productPage1’).getProduct().then((results)=\>{  
**let** id = results.\_id  
console.log(id)  
**return** getcollection(id)  
})  
.then((result)=\>{  
console.log(result)  
$w(‘#element’).text = result  
})  
})

---

<div class="post-metadata">

**Author:** ![liadshookroon](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/liadshookroon/32/22802_2.png) [@liadshookroon](https://forum.wixstudio.com/u/liadshookroon)\
**Post date:** [August 26, 2020, 9:04pm UTC](https://forum.wixstudio.com/t/code-example-get-product-collection-for-a-wix-stores-product/27093/2 "2020-08-26T21:04:27Z")

</div>

wow! thank you very much

---

<div class="post-metadata">

**Author:** ![dadonovaelena](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/dadonovaelena/32/24514_2.png) [@dadonovaelena](https://forum.wixstudio.com/u/dadonovaelena)\
**Post date:** [November 1, 2020, 8:42am UTC](https://forum.wixstudio.com/t/code-example-get-product-collection-for-a-wix-stores-product/27093/3 "2020-11-01T08:42:49Z")

</div>

Thanks!! I was just struggling to get a collection, since the box with sizes had to be different for women’s and men’s clothing. I really spent a week looking for different options. And thanks to you, everything worked for me!

---

<div class="post-metadata">

**Author:** ![verbaldancing](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/verbaldancing/32/10074_2.png) [@verbaldancing](https://forum.wixstudio.com/u/verbaldancing)\
**Post date:** [November 1, 2020, 9:52am UTC](https://forum.wixstudio.com/t/code-example-get-product-collection-for-a-wix-stores-product/27093/4 "2020-11-01T09:52:24Z")

</div>

I’m so glad to hear that, Elena! I thought others might be looking for the same solution. Wishing you much success with your shop, Michiel

---

<div class="post-metadata">

**Author:** ![info69353](https://avatars.discourse-cdn.com/v4/letter/i/ec9cab/32.png) [@info69353](https://forum.wixstudio.com/u/info69353)\
**Post date:** [February 1, 2023, 2:48am UTC](https://forum.wixstudio.com/t/code-example-get-product-collection-for-a-wix-stores-product/27093/5 "2023-02-01T02:48:43Z")

</div>

Wowwwwww! Thank you, I have been needing this for months!
