How to use RoutingService with cxRoute 'search' to show products of a specific category

Viewed 176

I would like to use the RoutingService to show products of a specific category on the search page. Sadly I could not find any documentation for the RoutingService on the generated api doc.

Spartacus Version: 1.5.1

This is what my current approach is looking like:

this.routingService.go({
            cxRoute: 'search',
            params: {
              query: '',
            },
          },
          {},
          {
            queryParams: {
              query: ':relevance:category:' + categoryId
            }
          },
        );

Currently this will result in the search page returning all products (not filtered by specific category)

Am I missing something? Or is this approach currently not possible and products can only be filtered via category when using cxRoute: 'category'?

1 Answers

I tried the code snippet you posted and it routes me to a search page with the correct category selected. I am not sure why you are not getting the same result.

For example try going to : https://spartacus.c39j2-walkersde1-d4-public.model-t.cc.commerce.ondemand.com/electronics-spa/en/USD/search?query=:relevance:category:1201

An alternative could be to use: /category/:code which would return that category which is the same as the search page.

(Edit): The following is for a free text search What we do in the search is pass the "query" so in this case the category in params:

this.routingService.go({
      cxRoute: 'search',
      params: { query },
    });

You can see the full code here https://sap.github.io/spartacus/injectables/SearchBoxComponentService.html#source

Related