Page 1 of 1

Current category recognition in Product controller

Posted: Mon Jul 07, 2014 2:06 pm
by saulius
Hi every one,

I would like to know if there are any nice way to retrieve current category while navigating to selected product?

Let say I have two categories:

1. Fruits;
2. Vegetables;

And I have one product which belongs to both categories: fruit and vegetable;

1. Let say it's a Tomato :).

So, user navigates to "Vegetables" category and sees here is a Tomato. He likes Tomatoes so he decides to buy it :).

So he click on Tomato product and navigates to it's description view.

At this point I would like to display a breadcrumb for my visitor so he knows in what category he came:

AllCategories -> Vegetables: Tomato

The problem is, that I can't just take myProduct.getDefaultCategory() as this might be Fruits category or other (if Tomato would belong to more than these two categories).

The only thing I could think off is to insert category ID value into product link, and then resolve it by overriding ProductHandlerMapping service and adding currentCategory model attribute for ProductController. But this does'nt look very nice to mee.

Maybe you know some better way to do it. Thanks in advance ;)

Re: Current category recognition in Product controller

Posted: Mon Jul 07, 2014 11:09 pm
by bpolster
You might find it easier to include the URL parameter but create a custom Thymeleaf processor to build your bookmark taking into account that parameter if it exists.

I've also seen implementations that right a cookie to store the last (x) categories that a user accessed and then use that in there bookmark logic. (e.g. if the recent history contains a valid parent category then use it, otherwise build the breadcrumb using the default).

This can be a tricky problem, let us know how it turns out or if you have a suggestion or pull request for the framework to better provide this feature.