Search in sources :

Example 1 with UpdateCartResponse

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

the class UpdateCartServiceImpl method copyCartToCurrentContext.

@Override
public UpdateCartResponse copyCartToCurrentContext(Order currentCart) {
    if (currentCart.getOrderItems() == null) {
        return null;
    }
    BroadleafCurrency currency = findActiveCurrency();
    if (currency == null) {
        return null;
    }
    // Reprice order logic
    List<OrderItemRequestDTO> itemsToReprice = new ArrayList<>();
    List<OrderItem> itemsToRemove = new ArrayList<>();
    List<OrderItem> itemsToReset = new ArrayList<>();
    boolean repriceOrder = true;
    for (OrderItem orderItem : currentCart.getOrderItems()) {
        // Lookup price in price list, if null, then add to itemsToRemove
        if (orderItem instanceof DiscreteOrderItem) {
            DiscreteOrderItem doi = (DiscreteOrderItem) orderItem;
            if (checkAvailabilityInLocale(doi, currency)) {
                OrderItemRequestDTO itemRequest = new OrderItemRequestDTO();
                itemRequest.setProductId(doi.getProduct().getId());
                itemRequest.setQuantity(doi.getQuantity());
                itemsToReprice.add(itemRequest);
                itemsToReset.add(orderItem);
            } else {
                itemsToRemove.add(orderItem);
            }
        } else if (orderItem instanceof BundleOrderItem) {
            BundleOrderItem boi = (BundleOrderItem) orderItem;
            for (DiscreteOrderItem doi : boi.getDiscreteOrderItems()) {
                if (checkAvailabilityInLocale(doi, currency)) {
                    OrderItemRequestDTO itemRequest = new OrderItemRequestDTO();
                    itemRequest.setProductId(doi.getProduct().getId());
                    itemRequest.setQuantity(doi.getQuantity());
                    itemsToReprice.add(itemRequest);
                    itemsToReset.add(orderItem);
                } else {
                    itemsToRemove.add(orderItem);
                }
            }
        }
    }
    for (OrderItem orderItem : itemsToReset) {
        try {
            currentCart = orderService.removeItem(currentCart.getId(), orderItem.getId(), false);
        } catch (RemoveFromCartException e) {
            LOG.error("Could not remove from cart.", e);
        }
    }
    for (OrderItemRequestDTO itemRequest : itemsToReprice) {
        try {
            currentCart = orderService.addItem(currentCart.getId(), itemRequest, false);
        } catch (AddToCartException e) {
            LOG.error("Could not add to cart.", e);
        }
    }
    // Reprice and save the cart
    try {
        currentCart = orderService.save(currentCart, repriceOrder);
    } catch (PricingException e) {
        LOG.error("Could not save cart.", e);
    }
    setSavedCurrency(currency);
    UpdateCartResponse updateCartResponse = new UpdateCartResponse();
    updateCartResponse.setRemovedItems(itemsToRemove);
    updateCartResponse.setOrder(currentCart);
    return updateCartResponse;
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) ArrayList(java.util.ArrayList) OrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO) BroadleafCurrency(org.broadleafcommerce.common.currency.domain.BroadleafCurrency) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) PricingException(org.broadleafcommerce.core.pricing.service.exception.PricingException) UpdateCartResponse(org.broadleafcommerce.core.order.service.call.UpdateCartResponse) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) AddToCartException(org.broadleafcommerce.core.order.service.exception.AddToCartException) RemoveFromCartException(org.broadleafcommerce.core.order.service.exception.RemoveFromCartException)

Aggregations

ArrayList (java.util.ArrayList)1 BroadleafCurrency (org.broadleafcommerce.common.currency.domain.BroadleafCurrency)1 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)1 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)1 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)1 OrderItemRequestDTO (org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO)1 UpdateCartResponse (org.broadleafcommerce.core.order.service.call.UpdateCartResponse)1 AddToCartException (org.broadleafcommerce.core.order.service.exception.AddToCartException)1 RemoveFromCartException (org.broadleafcommerce.core.order.service.exception.RemoveFromCartException)1 PricingException (org.broadleafcommerce.core.pricing.service.exception.PricingException)1