Page 1 of 1

Guest Cart not showing after login in Liferay

Posted: Tue Jun 04, 2013 7:14 am
by jasoneasterday
I'm having an issue with a guest adding items to the cart and then logs in. After the user logs in within Liferay, his old cart is retrieved and not the guest cart that he was using 2 mins prior. Please advise what I'm missing. Thanks.

Re: Guest Cart not showing after login in Liferay

Posted: Tue Jun 04, 2013 11:32 am
by phillipuniverse
So what's probably happening here is that within Liferay, the correct events are not being invoked. Specifically, the MergeCartProcessor is probably not being called on successful login. In a normal Spring web application, this gets invoked when a user is logged in.

To provide the correct functionality in Liferay, you will need to throw in a hook point to execute right when a user is changed from anonymous to authenticated. I'm not sure off the top of my head of how to do this with Liferay, especially since you will need to do this within the context of the Broadleaf portlet application so that you can inject the blMergeCartService and invoke it from there.

An example of how we do this in core Broadleaf is the BroadleafRegisterController. Here's the block of code that does it:

Code: Select all


    
@Resource(name="blMergeCartProcessor")
    protected 
MergeCartProcessor mergeCartProcessor;

    ...

    
registerCustomerValidator.validate(registerCustomerFormerrorsuseEmailForLogin);
        if (!
errors.hasErrors()) {
            
Customer newCustomer customerService.registerCustomer(registerCustomerForm.getCustomer(), 
                    
registerCustomerForm.getPassword(), registerCustomerForm.getPasswordConfirm());
            
assert(newCustomer != null);
            
            
// The next line needs to use the customer from the input form and not the customer returned after registration
            // so that we still have the unencoded password for use by the authentication mechanism.
            
Authentication auth loginService.loginCustomer(registerCustomerForm.getCustomer());
            
mergeCartProcessor.execute(requestresponseauth);            
            
            
String redirectUrl registerCustomerForm.getRedirectUrl();
            if (
StringUtils.isNotBlank(redirectUrl) && redirectUrl.contains(":")) {
                
redirectUrl null;
            }
            return 
StringUtils.isBlank(redirectUrl) ? getRegisterSuccessView() : "redirect:" redirectUrl;
        } else {
            return 
getRegisterView();
        }
 


In 2.2 versions of Broadleaf and above, 'mergeCart' is a bit of a misnomer as it doesn't actually merge anything. It just takes the cart that was previously saved in the database for the Customer and sets that to a named cart (a wish list) and then sets whatever cart they have as a guest to the current cart. No merge of cart items actually takes place.