Search code examples
javasap-commerce-cloud

How can show when I am logging in for the first time in the storefront - T&C popup in hybris?


How can show when I am logging in for the first time in the storefront - T&C popup in hybris? For example I am a new customer and I am loggin in store front for the first time, then I will see a popup with some "T&C of use" that I must check to be able to enter the shop. Maybe I must have some flag whom I say:

private boolean flag = false;
if user is login for first time
    flag = false;
        if(flag == false){
          show me pop up with T&C
          flag = true;
        }

But how can I get this last login or maybe have another way to do this?


Solution

  • Why not create a boolean flag acceptedTAC on customer type? If someone requests a page on your storefront who has this flag set to null or false, you can show this popup. When the user clicks the accept button, do an AJAX request to your server and set the acceptedTAC flag to true.

    This way you even have an "evidence" that a user accepted the TAC. Additionally you can query your database for users who did not yet accept the TAC.

    However the usual way you would force the user to accept the TAC would be during registration. A user can only register when he/she accepts the TAC.

    Here are the necessary steps:

    myextension-items.xml

    <itemtype code="Customer" ...>
      <attributes>
        <attribute name="acceptedTermsAndConditions" type="java.lang.Boolean">
          ..
        </attribute>
      <attributes>
    </itemtype>
    

    ShowTermsAndConditionsPopupBeforeViewHandler

    public class ShowTermsAndConditionsPopupBeforeViewHandler implements BeforeViewHandler {
    
      @Resource 
      UserService userService;
    
      @Override
      public void beforeView(HttpServletRequest request, HttpServletResponse response, ModelAndView modelAndView) {
        UserModel user = userService.getCurrentUser();
        if (user instanceof CustomerModel && !userService.isAnonymousUser(user)) {
          CustomerModel customer = (CustomerModel) user;
          modelAndView.addObject("showTermsAndConditionsPopup", BooleanUtils.isNotTrue(customer.isTermsAndConditionsAccepted()));
        } else {
          modelAndView.addObject("showTermsAndConditionsPopup", false);
        }
      }
    }
    

    Register BeforeViewHandler in spring-mvc-config.xml

    ...
    
    <util:list id="defaultBeforeViewHandlersList">
    ...
        <bean class="my.package.ShowTermsAndConditionsPopupBeforeViewHandler"/>
    ...
    </util:list>
    ...
    

    Create JavaScript Variable in javaScriptVariables.tag

    ...
    ACC.config.showTermsAndConditionsPopup=${showTermsAndConditionsPopup};
    ...
    

    Add logic to open popup in JavaScript

    ...
    if(ACC.config.showTermsAndConditionsPopup) {
       showPopup();
    }
    ...
    

    Create popup content with form:

    <c:url var="url" value="/acceptTermsAndConditions" />
    <form action="${url}" method="POST">
      <label for="acceptTermsAndConditions">I accept Terms and Conditions</label>
      <input type="checkbox" id="acceptTermsAndConditions" name="acceptTermsAndConditions" />
      <button type="submit>Submit</button> 
    </form> 
    

    Create TermsAndConditionsController

    @Controller
    public TermsAndConditionsController {
    
      @Resource 
      private UserService userService;
      @Resource 
      private ModelService modelService;
    
      @RequestMapping(value = "/acceptTermsAndConditions", method = RequestMethod.POST) 
      @ResponseBody
      @ResponseStatus(value = HttpStatus.OK)
      public void acceptTermsAndConditions() {
        UserModel user = userService.getCurrentUser();
        if (user instanceof CustomerModel && !userService.isAnonymousUser(user)) {
          CustomerModel customer = (CustomerModel) user;
          customer.setAcceptedTermsAndConditions(true);
          modelService.save(customer);
        }
      }
    }