Search in sources :

Example 6 with MergeCartResponse

use of org.broadleafcommerce.core.order.service.call.MergeCartResponse in project BroadleafCommerce by BroadleafCommerce.

the class LegacyMergeCartServiceImpl method mergeCart.

@Override
public MergeCartResponse mergeCart(Customer customer, Order anonymousCart, boolean priceOrder) throws PricingException {
    MergeCartResponse mergeCartResponse = new MergeCartResponse();
    // reconstruct cart items (make sure they are valid)
    ReconstructCartResponse reconstructCartResponse = reconstructCart(customer, false);
    mergeCartResponse.setRemovedItems(reconstructCartResponse.getRemovedItems());
    Order customerCart = reconstructCartResponse.getOrder();
    if (anonymousCart != null && customerCart != null && anonymousCart.getId().equals(customerCart.getId())) {
        /*
             * Set merged to false if the cart ids are equal (cookied customer
             * logs in).
             */
        mergeCartResponse.setMerged(false);
    } else {
        /*
             * Set the response to merged if the saved cart has any items
             * available to merge in.
             */
        mergeCartResponse.setMerged(customerCart != null && customerCart.getOrderItems().size() > 0);
    }
    // add anonymous cart items (make sure they are valid)
    if (anonymousCart != null && (customerCart == null || !customerCart.getId().equals(anonymousCart.getId()))) {
        if (anonymousCart != null && anonymousCart.getOrderItems() != null && !anonymousCart.getOrderItems().isEmpty()) {
            if (customerCart == null) {
                customerCart = orderService.createNewCartForCustomer(customer);
            }
            Map<OrderItem, OrderItem> oldNewItemMap = new HashMap<OrderItem, OrderItem>();
            customerCart = mergeRegularOrderItems(anonymousCart, mergeCartResponse, customerCart, oldNewItemMap);
            customerCart = mergeOfferCodes(anonymousCart, customerCart);
            customerCart = removeExpiredGiftWrapOrderItems(mergeCartResponse, customerCart, oldNewItemMap);
            customerCart = mergeGiftWrapOrderItems(mergeCartResponse, customerCart, oldNewItemMap);
            orderService.cancelOrder(anonymousCart);
        }
    }
    // copy the customer's email to this order, overriding any previously set email
    if (customerCart != null && StringUtils.isNotBlank(customer.getEmailAddress())) {
        customerCart.setEmailAddress(customer.getEmailAddress());
        customerCart = orderService.save(customerCart, priceOrder);
    }
    mergeCartResponse.setOrder(customerCart);
    return mergeCartResponse;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) HashMap(java.util.HashMap) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) GiftWrapOrderItem(org.broadleafcommerce.core.order.domain.GiftWrapOrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) ReconstructCartResponse(org.broadleafcommerce.core.order.service.call.ReconstructCartResponse) MergeCartResponse(org.broadleafcommerce.core.order.service.call.MergeCartResponse)

Example 7 with MergeCartResponse

use of org.broadleafcommerce.core.order.service.call.MergeCartResponse in project BroadleafCommerce by BroadleafCommerce.

the class CartStateRequestProcessor method mergeCart.

/**
 * Looks up the anonymous customer and merges that cart with the cart from the given logged in <b>customer</b>. This
 * will also remove the customer from session after it has finished since it is no longer needed
 */
public Order mergeCart(Customer customer, WebRequest request) {
    Customer anonymousCustomer = customerStateRequestProcessor.getAnonymousCustomer(request);
    MergeCartResponse mergeCartResponse;
    try {
        Order cart = orderService.findCartForCustomer(anonymousCustomer);
        mergeCartResponse = mergeCartService.mergeCart(customer, cart);
    } catch (PricingException e) {
        throw new RuntimeException(e);
    } catch (RemoveFromCartException e) {
        throw new RuntimeException(e);
    }
    if (BLCRequestUtils.isOKtoUseSession(request)) {
        // The anonymous customer from session is no longer needed; it can be safely removed
        request.removeAttribute(CustomerStateRequestProcessor.getAnonymousCustomerSessionAttributeName(), WebRequest.SCOPE_GLOBAL_SESSION);
        request.removeAttribute(CustomerStateRequestProcessor.getAnonymousCustomerIdSessionAttributeName(), WebRequest.SCOPE_GLOBAL_SESSION);
        request.setAttribute(mergeCartResponseKey, mergeCartResponse, WebRequest.SCOPE_GLOBAL_SESSION);
    }
    return mergeCartResponse.getOrder();
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) PricingException(org.broadleafcommerce.core.pricing.service.exception.PricingException) Customer(org.broadleafcommerce.profile.core.domain.Customer) MergeCartResponse(org.broadleafcommerce.core.order.service.call.MergeCartResponse) RemoveFromCartException(org.broadleafcommerce.core.order.service.exception.RemoveFromCartException)

Example 8 with MergeCartResponse

use of org.broadleafcommerce.core.order.service.call.MergeCartResponse in project BroadleafCommerce by BroadleafCommerce.

the class MergeCartProcessorImpl method execute.

@Override
public void execute(WebRequest request, Authentication authResult) {
    Customer loggedInCustomer = customerService.readCustomerByUsername(authResult.getName());
    Customer anonymousCustomer = customerStateRequestProcessor.getAnonymousCustomer(request);
    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);
    }
    if (BLCRequestUtils.isOKtoUseSession(request)) {
        request.setAttribute(mergeCartResponseKey, mergeCartResponse, WebRequest.SCOPE_GLOBAL_SESSION);
    }
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) PricingException(org.broadleafcommerce.core.pricing.service.exception.PricingException) Customer(org.broadleafcommerce.profile.core.domain.Customer) MergeCartResponse(org.broadleafcommerce.core.order.service.call.MergeCartResponse) RemoveFromCartException(org.broadleafcommerce.core.order.service.exception.RemoveFromCartException)

Aggregations

Order (org.broadleafcommerce.core.order.domain.Order)8 MergeCartResponse (org.broadleafcommerce.core.order.service.call.MergeCartResponse)8 Customer (org.broadleafcommerce.profile.core.domain.Customer)6 Transactional (org.springframework.transaction.annotation.Transactional)4 Test (org.testng.annotations.Test)4 ReconstructCartResponse (org.broadleafcommerce.core.order.service.call.ReconstructCartResponse)2 RemoveFromCartException (org.broadleafcommerce.core.order.service.exception.RemoveFromCartException)2 PricingException (org.broadleafcommerce.core.pricing.service.exception.PricingException)2 HashMap (java.util.HashMap)1 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)1 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)1 GiftWrapOrderItem (org.broadleafcommerce.core.order.domain.GiftWrapOrderItem)1 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)1