use of org.broadleafcommerce.core.pricing.service.exception.PricingException 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;
}
Aggregations