@Override protected BigDecimal getValue(Map<Purchasable, Long> items) { BigDecimal itemsTotal = BigDecimal.ZERO; for (Purchasable purchasable : items.keySet()) { BigDecimal itemTotal = purchasable.getUnitPrice().multiply(BigDecimal.valueOf(items.get(purchasable))); itemsTotal = itemsTotal.add(itemTotal); } return itemsTotal; } }
@Override protected BigDecimal getValue(Map<Purchasable, Long> items) { BigDecimal weight = BigDecimal.ZERO; for (Purchasable purchasable : items.keySet()) { try { weight = weight.add(purchasable.getActualWeight().isPresent() ? purchasable.getActualWeight().get().multiply(BigDecimal.valueOf(items.get(purchasable))) : BigDecimal.ZERO); } catch (ClassCastException e) { throw new IllegalArgumentException( "Cannot calculate weight-based shipping price for something else than a product", e); } } return weight; } }
if (purchasable.getParent().isPresent() && purchasable.getParent().get().isLoaded()) { rootPurchasable = purchasable.getParent().get().get(); title = rootPurchasable.getTitle() + " - " + purchasable.getTitle(); } else { rootPurchasable = purchasable; title = purchasable.getTitle(); final Product product = productStore.findById(rootPurchasable.getId()); final Product purchasedProduct = productStore.findById(purchasable.getId()); EntityData<Product> productData = dataLoader.load(product); Optional<Tenant> tenant = productData.getData(Tenant.class);
public BigDecimal getItemTotal(Taxable item) { BigDecimal total = BigDecimal.ZERO; BigDecimal unitPrice = item.getUnitPrice(); if (unitPrice == null && item.getParent().isPresent() && item.getParent().get().isLoaded()) { unitPrice = item.getParent().get().get().getUnitPrice(); } if (items.containsKey(item) && unitPrice != null && items.get(item) > 0) { total = total.add(unitPrice.multiply(BigDecimal.valueOf(items.get(item)))); } return total; }
public void addItem(Taxable item, Long quantity) { LOGGER.debug("Adding item {} ({}) to cart", item, quantity); Preconditions.checkNotNull(item); Preconditions.checkNotNull(quantity); Preconditions.checkNotNull(item.getId()); Preconditions.checkNotNull(quantity); Preconditions.checkArgument(quantity > 0); BigDecimal unitPrice = null; if (item.getUnitPrice() != null) { unitPrice = item.getUnitPrice(); } else if (item.getParent().isPresent() && item.getParent().get().isLoaded()) { unitPrice = item.getParent().get().get().getUnitPrice(); } Preconditions.checkNotNull(unitPrice); Preconditions.checkArgument(unitPrice.compareTo(BigDecimal.ZERO) > 0); if (items.containsKey(item)) { Long newQuantity = items.get(item) + quantity; items.put(item, newQuantity); } else { items.put(item, quantity); } LOGGER.debug("Cart now contains {} items", this.items.size()); }
public void setItem(Taxable item, Long quantity) { Preconditions.checkNotNull(item); Preconditions.checkNotNull(quantity); Preconditions.checkNotNull(item.getId()); Preconditions.checkNotNull(quantity); Preconditions.checkArgument(quantity > 0); BigDecimal unitPrice = null; if (item.getUnitPrice() != null || !item.getParent().isPresent()) { unitPrice = item.getUnitPrice(); } else if (item.getParent().get().isLoaded()) { unitPrice = item.getParent().get().get().getUnitPrice(); } Preconditions.checkNotNull(unitPrice, "Can't set cart item with no unit price"); Preconditions.checkArgument(unitPrice.compareTo(BigDecimal.ZERO) > 0); items.put(item, quantity); }