How to get the current category information in SAP Spartacus category page

Time:2021-7-29

demand

https://stackoverflow.com/que…

Implementation principle

AFAIK, currently we do not have anything similar to CurrentProductService for current category.

The current SAP Spartacus standard implementation lacks the support of current productservice for product details pages.

Looking at the implementation of CurrentProductService you can see that it is using RoutingService and it’s getRouterState() method (picture 1).

After studying the implementation code of currentproductservice, it is found that its implementation principle is to first use routingservice to get the product code of the current page from routerstate, and then use another API, productservice, to read the detailed data of the product.

How to get the current category information in SAP Spartacus category page

You can create something similar for current category, because the router state consists of category informations (as seen below on picture 2).

Therefore, we can take a similar idea to extract the category details from the state of the category page:

  • categoryCode
  • param0, param1, param2

How to get the current category information in SAP Spartacus category page

More Jerry’s original articles are: “Wang Zixi”:
How to get the current category information in SAP Spartacus category page