How to set B2B Unit in session (Hybris)?

Viewed 1178

How can I set B2B Unit in session. I have facade class which implements logic to set units in session (ex. When one user is login in storefront give me his units and set these units in session). This is my method in custom class which extedns DefaultCustomerFacade.class

 @Override
    public void loginSuccess(){
        Set<B2BUnitModel> allUserGroupsForUser = getUserService().getAllUserGroupsForUser(getCurrentUser(), B2BUnitModel.class);
        List<String> b2bunitIds = allUserGroupsForUser.stream()
                .map(B2BUnitModel::getName)
                .collect(Collectors.toList());
        if(!allUserGroupsForUser.isEmpty()) {
            getSessionService().setAttribute("availableB2BUnits", b2bunitIds);
        }
        super.loginSuccess();
    }

Then in storefront, I showed in select tag in jsp, drop-down with these units. Ok, so I have attribute now with these units which is in session and I want when I click for some B2B Unit, this one unit to set in session. I think, I shoud do some controller to implement logic ( set currentUnit in session ) and in one js file to make some ajax call with some method onChange, or some ajax api, but I don't know how to do this. Can someone help me, i am new in hybris and I will be glad if someone help me

1 Answers

You have already halfway through as you have already populated availableB2BUnits from the backend(Façade) to the frontend(JSP). Similarly, you have to send back the "b2bunitIds" to the backend.

Implement the following steps:

  1. Find/ create the javascript file from where you can make ajax (or normal post) call. Make sure that js file is loaded for the current page on which you need all these actions to be performed otherwise your javascript method won't be executed.

  2. Write required logic in above js file to obtain b2bunitId from the selection made. Use b2bunitId to invoke Controller's method (RequestMapping).

  3. From the invoked method of the Controller set the selected b2bunitId in session as you did it in the above method (getSessionService().setAttribute("availableB2BUnits", b2bunitIds);).

Related