Search in sources :

Example 1 with CartModificationException

use of com.salesmanager.shop.model.shoppingcart.CartModificationException in project shopizer by shopizer-ecommerce.

the class ShoppingCartFacadeImpl method updateCartItems.

// TODO promoCode request parameter
@Override
public ShoppingCartData updateCartItems(Optional<String> promoCode, final List<ShoppingCartItem> shoppingCartItems, final MerchantStore store, final Language language) throws Exception {
    Validate.notEmpty(shoppingCartItems, "shoppingCartItems null or empty");
    ShoppingCart cartModel = null;
    Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> cartItems = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
    for (ShoppingCartItem item : shoppingCartItems) {
        if (item.getQuantity() < 1) {
            throw new CartModificationException("Quantity must not be less than one");
        }
        if (cartModel == null) {
            cartModel = getCartModel(item.getCode(), store);
        }
        com.salesmanager.core.model.shoppingcart.ShoppingCartItem entryToUpdate = getEntryToUpdate(item.getId(), cartModel);
        if (entryToUpdate == null) {
            throw new CartModificationException("Unknown entry number.");
        }
        entryToUpdate.getProduct();
        LOG.info("Updating cart entry quantity to" + item.getQuantity());
        entryToUpdate.setQuantity((int) item.getQuantity());
        List<ProductAttribute> productAttributes = new ArrayList<ProductAttribute>();
        productAttributes.addAll(entryToUpdate.getProduct().getAttributes());
        final FinalPrice finalPrice = productPriceUtils.getFinalProductPrice(entryToUpdate.getProduct(), productAttributes);
        entryToUpdate.setItemPrice(finalPrice.getFinalPrice());
        cartItems.add(entryToUpdate);
    }
    cartModel.setPromoCode(null);
    if (promoCode.isPresent()) {
        cartModel.setPromoCode(promoCode.get());
        cartModel.setPromoAdded(new Date());
    }
    cartModel.setLineItems(cartItems);
    shoppingCartService.saveOrUpdate(cartModel);
    LOG.info("Cart entry updated with desired quantity");
    ShoppingCartDataPopulator shoppingCartDataPopulator = new ShoppingCartDataPopulator();
    shoppingCartDataPopulator.setShoppingCartCalculationService(shoppingCartCalculationService);
    shoppingCartDataPopulator.setPricingService(pricingService);
    shoppingCartDataPopulator.setimageUtils(imageUtils);
    return shoppingCartDataPopulator.populate(cartModel, store, language);
}
Also used : CartModificationException(com.salesmanager.shop.model.shoppingcart.CartModificationException) ShoppingCartDataPopulator(com.salesmanager.shop.populator.shoppingCart.ShoppingCartDataPopulator) ArrayList(java.util.ArrayList) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) Date(java.util.Date) LocalDate(java.time.LocalDate) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart) PersistableShoppingCartItem(com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem) ShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ShoppingCartItem) HashSet(java.util.HashSet) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice)

Example 2 with CartModificationException

use of com.salesmanager.shop.model.shoppingcart.CartModificationException in project shopizer by shopizer-ecommerce.

the class ShoppingCartFacadeImpl method updateCartItem.

@Override
public ShoppingCartData updateCartItem(final Long itemID, final String cartId, final long newQuantity, final MerchantStore store, final Language language) throws Exception {
    if (newQuantity < 1) {
        throw new CartModificationException("Quantity must not be less than one");
    }
    if (StringUtils.isNotBlank(cartId)) {
        ShoppingCart cartModel = getCartModel(cartId, store);
        if (cartModel != null) {
            com.salesmanager.core.model.shoppingcart.ShoppingCartItem entryToUpdate = getEntryToUpdate(itemID.longValue(), cartModel);
            if (entryToUpdate == null) {
                throw new CartModificationException("Unknown entry number.");
            }
            entryToUpdate.getProduct();
            LOG.info("Updating cart entry quantity to" + newQuantity);
            entryToUpdate.setQuantity((int) newQuantity);
            List<ProductAttribute> productAttributes = new ArrayList<ProductAttribute>();
            productAttributes.addAll(entryToUpdate.getProduct().getAttributes());
            final FinalPrice finalPrice = productPriceUtils.getFinalProductPrice(entryToUpdate.getProduct(), productAttributes);
            entryToUpdate.setItemPrice(finalPrice.getFinalPrice());
            shoppingCartService.saveOrUpdate(cartModel);
            LOG.info("Cart entry updated with desired quantity");
            ShoppingCartDataPopulator shoppingCartDataPopulator = new ShoppingCartDataPopulator();
            shoppingCartDataPopulator.setShoppingCartCalculationService(shoppingCartCalculationService);
            shoppingCartDataPopulator.setPricingService(pricingService);
            shoppingCartDataPopulator.setimageUtils(imageUtils);
            return shoppingCartDataPopulator.populate(cartModel, store, language);
        }
    }
    return null;
}
Also used : CartModificationException(com.salesmanager.shop.model.shoppingcart.CartModificationException) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart) ShoppingCartDataPopulator(com.salesmanager.shop.populator.shoppingCart.ShoppingCartDataPopulator) ArrayList(java.util.ArrayList) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice)

Aggregations

ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)2 FinalPrice (com.salesmanager.core.model.catalog.product.price.FinalPrice)2 ShoppingCart (com.salesmanager.core.model.shoppingcart.ShoppingCart)2 CartModificationException (com.salesmanager.shop.model.shoppingcart.CartModificationException)2 ReadableShoppingCart (com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart)2 ShoppingCartDataPopulator (com.salesmanager.shop.populator.shoppingCart.ShoppingCartDataPopulator)2 ArrayList (java.util.ArrayList)2 PersistableShoppingCartItem (com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem)1 ShoppingCartItem (com.salesmanager.shop.model.shoppingcart.ShoppingCartItem)1 LocalDate (java.time.LocalDate)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1