Page 1 of 1

cart not merged when customer log in

Posted: Wed Mar 20, 2013 10:39 am
by denis
Hi,

I don't know if it's a bug or if it's not possible but when i add an item into my cart with an anonymous customer, then if i log in with a registeted customer the cart is not merged, it still empty.

Is it possible to keep the cart state when a customer log in?

Thanks

Re: cart not merged when customer log in

Posted: Wed Mar 20, 2013 11:34 am
by denis
Problem solved, it's a bug in method excute of MergeCartProcessorImpl :

Code: Select all

public void execute(WebRequest request, Authentication authResult) {
        Customer loggedInCustomer = customerService.readCustomerByUsername(authResult.getName());
//Here       
Customer anonymousCustomer = (Customer) request.getAttribute(CustomerStateRequestProcessor.ANONYMOUS_CUSTOMER_SESSION_ATTRIBUTE_NAME, WebRequest.SCOPE_GLOBAL_SESSION);
       
        Order cart = null;
        if (anonymousCustomer != null) {
            cart = orderService.findCartForCustomer(anonymousCustomer);
        }
        MergeCartResponse mergeCartResponse;
        try {
            mergeCartResponse = mergeCartService.mergeCart(loggedInCustomer, cart);
        } catch (PricingException e) {
            throw new RuntimeException(e);
        } catch (RemoveFromCartException e) {
            throw new RuntimeException(e);
        }

        request.setAttribute(mergeCartResponseKey, mergeCartResponse, WebRequest.SCOPE_GLOBAL_SESSION);
    }


instead of :

Code: Select all

Customer anonymousCustomer = (Customer) request.getAttribute(CustomerStateRequestProcessor.ANONYMOUS_CUSTOMER_SESSION_ATTRIBUTE_NAME, WebRequest.SCOPE_GLOBAL_SESSION);


You should use :

Code: Select all

Customer anonymousCustomer = (Customer) request.getAttribute(CustomerStateRequestProcessor.getCustomerRequestAttributeName(), WebRequest.SCOPE_GLOBAL_SESSION);


because we have the possibility to override the method "getCustomerRequestAttributeName()".