public void ApplyOfferVoucher(OfferVoucher offerVoucher) { if (_offerVouchers.Any(x => x.Code == offerVoucher.Code)) { throw new Exception($"Basket already has Offer Voucher with code '{offerVoucher.Code}' applied"); } _offerVouchers.Add(offerVoucher); IsDirty = true; }
private decimal GetVoucherOfferDiscount(OfferVoucher voucher) { var discount = voucher.Amount; if (voucher.Category != null) { // This is a voucher for a specific product category. So get those products var categoryProducts = _products.Where(x => x.Product.Category == voucher.Category).ToList(); if (categoryProducts.Count == 0) { _messages.Add($"There are no products in your basket applicable to voucher Voucher {voucher.Code}"); return(0.00m); } // See what the total of the qualifying category vouchers is var categoryProductsTotal = categoryProducts.Sum(x => x.Total); // if the qualifying total is less than the voucher then use the qualifying total discount = voucher.Amount >= categoryProductsTotal ? categoryProductsTotal : voucher.Amount; } return(discount); }