Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to add new filters? #554

Open
macrozone opened this issue Mar 27, 2023 · 8 comments
Open

how to add new filters? #554

macrozone opened this issue Mar 27, 2023 · 8 comments
Milestone

Comments

@macrozone
Copy link
Contributor

How can i add a custom filter which i can pass as key-value to filterQuery ?

i have a custom meta data on a product and i want to add a filter that filters by that. how can i do that?

there is a transformProductSelector function, but its not called (or only with undefined key and value), so i assume it uses some caching. I don't see anything in the docs how to update this cache for all existing products.

@Mikearaya
Copy link
Contributor

@macrozone didn't you find it useful, https://docs.unchained.shop/advanced-config/filter/

@macrozone
Copy link
Contributor Author

@macrozone didn't you find it useful, https://docs.unchained.shop/advanced-config/filter/

unfortunatly not, i extended transformProductSelector to include my new key, but this function is never called with any key or value, maybe its cached?

@pozylon do you have an idea?

@pozylon
Copy link
Member

pozylon commented Mar 29, 2023

@macrozone Yes it's cached, the cache is invalidated for products and categories that are changed and on every start/restart of the app unless you have set "skipInvalidationOnStartup" to true. For performance reasons when you have a lot of filters, products and categories you don't want to run the cache invalidation on boot.

@macrozone
Copy link
Contributor Author

skipInvalidationOnStartup

that was the missing piece.

would be be good to have a command for that instead ideally.

@macrozone macrozone reopened this Mar 29, 2023
@macrozone
Copy link
Contributor Author

i m sorry, i closed this prematurly.

I could not make it work.

Some step is missing in the documentation. It seems all filters need to be seeded first in the database, and even that is not enough to make it work. Some piece is missing.

@pozylon i will update my MR with more information in the project.

@macrozone
Copy link
Contributor Author

additional information: i created the custom filter as well through the unchained control panel

@macrozone
Copy link
Contributor Author

macrozone commented Mar 30, 2023

ok i see now that i have to add this filter to each and very assortment. How can i add filters easily for all assortments?

and even if I add filters for each assortment, how can i also enable this filter for the generic search that does not take an assortment?

@pozylon
Copy link
Member

pozylon commented Apr 3, 2023

If you want to enable a global filter, you can implement transformFilterSelector and return additional _ids in the filter selector. this is the default implementation:

const defaultSelector = ({ filterIds, filterQuery, includeInactive }: SearchQuery) => {
  const selector: Query = {};
  const keys = (filterQuery || []).map((filter) => filter.key);
  if (Array.isArray(filterIds)) {
    // return predefined list
    selector._id = { $in: filterIds };
  } else if (keys.length > 0) {
    // return filters that are part of the filterQuery
    selector.key = { $in: keys };
  } else {
    // do not return filters
    return null;
  }
  if (!includeInactive) {
    // include only active filters
    selector.isActive = true;
  }
  return selector;
};

So you could do the following to enable a global filter when searching:

async transformFilterSelector(last) {
        // eslint-disable-line
        const { searchQuery = {} } = params;
        const { queryString, filterIds } = searchQuery;

        if (queryString && !filterIds) {
          return {
            ...(last || {}),
            _id: {
              $in: [
                GLOBAL_FILTER_ID,
              ],
            },
          };
        }

        return last;
      },

@pozylon pozylon added this to the v3.1 milestone Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants