Filter products by collection

hi,
i want to filter products in the store/product collection by store/collection
but i can find any collection column in the products table

anyone?

2 Likes

https://support.wix.com/en/article/adding-sort-and-filter-options-to-a-wix-stores-product-gallery
https://support.wix.com/en/article/creating-custom-filters-in-wix-stores

i need it in corvid (code)
there is collections and products table(DB)
but there is no column in the products table that connect the product to collection

i think wix forgot to add that column

I am facing the same problem. I also can’t find a way to filter a dataset by collection.

This is what I am trying to do and hoped it would worked but it doesn’t:

The function below is passed a string (“collection”) and a reference to the dataset (“dataset”). Then, I wanted to set a filter to the dataset so my repeater only shows the filtered items. But it is not possible to filter by collections (for example, products belonging to the “men’s” collection).

export function updateRepeater(collection, dataset){
    dataset.setFilter(wixData.filter()
        .contains("collections", collection)
    );
}

According to this page , the " collections" field is possible to be sorted using " .hasSome" . I have tried it in many different ways but it doesn’t work. Am I missing something?

hi,
they added the collection column in Stores/Products

 wixData.query("Stores/Products")
.eq("collections", "NewItemsCollection"))
.descending("name")
.limit(10)
.find()
.then((collectionItem) => {
let collectionItems=collectionItem
})

@naimibaby I tried this:

wixData.query("Stores/Products")
    .hasSome("collections", "sale")
    .find()
    .then((results) => {
        console.log(results);
    }, (error) => {
        console.log(error);
});

And also this:

wixData.query("Stores/Products")
    .eq("collections", "sale")
    .find()
    .then((results) => {
        console.log(results);
    }, (error) => {
        console.log(error);
});

None worked.

After reading the API I found the " .include() " method. Seems like the " .query() " method don’t return the referenced fields. To return them, we need to include them in the query. I have finally been able to get the query to return the “Collections” field. I had to do this:

wixData.query("Stores/Products")
    .include("collections")
    .find()
    .then((results) => {
        console.log(results);
    }, (error) => {
        console.log(error);
    });

I just don’t know how to filter a field that is a list of objects. For example, if the field collections is like this:

collections = [
    {
        "id": 0,
        "name": "sale"
    },
    {
        "id": 1,
        "name": "sale"    
    },
    {
        "id": 2,
        "name": "winter"    
    }
]

If this is the content of my collections field, how to filter only those whose collections list have an object with an attribute equals to “name”?

Note the collections field is a multi reference filed which the field will be stored as an array.
Type : Reference (Multiple Items)

So you can use hasSome provided that you use the function correctly.

https://www.wix.com/corvid/reference/wix-data.WixDataQuery.html#hasSome

  • If the value of the specified property is an array, hasSome() will match if any of the elements of that array match any of the specified values.

  • If the specified property contains multiple references, pass item IDs in the value property. In such a case, hasSome() will match if any of the multiple references match any of the specified ID values.

Examples
Add a has some filter to a query

let newQuery = query.hasSome("colors", ["red", "yellow", "blue"]);
// or
let newQuery = query.hasSome("colors", "red", "yellow", "blue");

Add a has some filter on a multiple reference field to a query

This example gets the items from the movies collection that have a reference in the authors field to an item with an ID that is either 1357 or 2468.

import wixData from 'wix-data';

// ...

wixData.query("movies")
  .hasSome("actors", ["1357", "2468"])
  .find()
  .then( (results) => {
    if(results.items.length > 0) {
      let items = results.items;
      let firstItem = items[0];
      let totalCount = results.totalCount;
      let pageSize = results.pageSize;
      let currentPage = results.currentPage;
      let totalPages = results.totalPages;
      let hasNext = results.hasNext();
      let hasPrev = results.hasPrev();
      let length = results.length;
      let query = results.query;
    } else {
      // handle case where no matching items found
    }
  } )
  .catch( (error) => {
    let errorMsg = error.message;
    let code = error.code;

Also, note that it is case sensitive too, so if a user types Milk, then it will look for Milk and not milk, so the searches need to match your array.
Matching strings with hasSome() is case sensitive, so “text” is not equal to “Text”.

As for using include, that has already been done if you had searched through previous forum posts.

You can do it without using code by using two datasets, as shown in this previous forum post here and in the last link from the list below too.

For more about using multi reference fields, then please read teh Wix pages about them if not done so already.

Finally, you can always look at using reference functions too, however they might not be suitable for your needs.

1 Like

Phewww… So, I was finally able to fetch product for a specific collection!

I got the idea from this example: https://www.wix.com/corvid/forum/community-discussion/example-wix-data-multiple-references
In queryHasSome() they are fetching “colors” which is similar to our “collections”.

The basic idea is that you can only search for the collections if you know the _id field.

So 1st query for mycollection:

var  mycollection = (await wixData.query('Stores/Collections')
                           .eq("name","Special Products")
                           .find()
                     ).items[0]

And then search the above collection using this code:

wixData.query("Stores/Products")
.include("collections")
.hasSome("collections", mycollection._id);

I hope this is helpful to others.

Cheers,
Jags