Search in sources :

Example 1 with OfferException

use of org.broadleafcommerce.core.offer.service.exception.OfferException in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafCartController method addPromo.

/**
 * Attempts to add provided Offer to Cart
 *
 * @param request
 * @param response
 * @param model
 * @param customerOffer
 * @return the return view
 * @throws IOException
 * @throws PricingException
 * @throws OfferMaxUseExceededException
 */
public String addPromo(HttpServletRequest request, HttpServletResponse response, Model model, String customerOffer) throws IOException, PricingException {
    Order cart = CartState.getCart();
    Boolean promoAdded = false;
    String exception = "";
    if (cart != null && !(cart instanceof NullOrderImpl)) {
        List<OfferCode> offerCodes = offerService.lookupAllOfferCodesByCode(customerOffer);
        if (CollectionUtils.isNotEmpty(offerCodes)) {
            for (OfferCode offerCode : offerCodes) {
                if (offerCode != null) {
                    try {
                        orderService.addOfferCode(cart, offerCode, false);
                        promoAdded = true;
                    } catch (OfferException e) {
                        if (e instanceof OfferMaxUseExceededException) {
                            exception = "Use Limit Exceeded";
                        } else if (e instanceof OfferExpiredException) {
                            exception = "Offer Has Expired";
                        } else if (e instanceof OfferAlreadyAddedException) {
                            exception = "Offer Has Already Been Added";
                        } else {
                            exception = "An Unknown Offer Error Has Occurred";
                        }
                    }
                } else {
                    exception = "Invalid Code";
                }
            }
            cart = orderService.save(cart, true);
        } else {
            exception = "Unknown Code";
        }
    } else {
        exception = "Invalid Cart";
    }
    if (isAjaxRequest(request)) {
        Map<String, Object> extraData = new HashMap<>();
        extraData.put("promoAdded", promoAdded);
        extraData.put("exception", exception);
        model.addAttribute("blcextradata", new ObjectMapper().writeValueAsString(extraData));
    } else {
        model.addAttribute("exception", exception);
    }
    return isCheckoutContext(request) ? getCheckoutView() : getCartView();
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) HashMap(java.util.HashMap) OfferException(org.broadleafcommerce.core.offer.service.exception.OfferException) NullOrderImpl(org.broadleafcommerce.core.order.domain.NullOrderImpl) OfferMaxUseExceededException(org.broadleafcommerce.core.offer.service.exception.OfferMaxUseExceededException) OfferCode(org.broadleafcommerce.core.offer.domain.OfferCode) OfferExpiredException(org.broadleafcommerce.core.offer.service.exception.OfferExpiredException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) OfferAlreadyAddedException(org.broadleafcommerce.core.offer.service.exception.OfferAlreadyAddedException)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 HashMap (java.util.HashMap)1 OfferCode (org.broadleafcommerce.core.offer.domain.OfferCode)1 OfferAlreadyAddedException (org.broadleafcommerce.core.offer.service.exception.OfferAlreadyAddedException)1 OfferException (org.broadleafcommerce.core.offer.service.exception.OfferException)1 OfferExpiredException (org.broadleafcommerce.core.offer.service.exception.OfferExpiredException)1 OfferMaxUseExceededException (org.broadleafcommerce.core.offer.service.exception.OfferMaxUseExceededException)1 NullOrderImpl (org.broadleafcommerce.core.order.domain.NullOrderImpl)1 Order (org.broadleafcommerce.core.order.domain.Order)1