Example #1
2
 public bool Applies(ShoppingCartQuantityProduct quantityProduct, IEnumerable<ShoppingCartQuantityProduct> cartProducts) {
     if (DiscountPart == null) return false;
     var now = _clock.UtcNow;
     if (DiscountPart.StartDate != null && DiscountPart.StartDate > now) return false;
     if (DiscountPart.EndDate != null && DiscountPart.EndDate < now) return false;
     if (DiscountPart.StartQuantity != null &&
         DiscountPart.StartQuantity > quantityProduct.Quantity)
         return false;
     if (DiscountPart.EndQuantity != null &&
         DiscountPart.EndQuantity < quantityProduct.Quantity)
         return false;
     if (!string.IsNullOrWhiteSpace(DiscountPart.Pattern)) {
         string path;
         if (DiscountPart.DisplayUrlResolver != null) {
             path = DiscountPart.DisplayUrlResolver(quantityProduct.Product);
         }
         else {
             var urlHelper = new UrlHelper(_wca.GetContext().HttpContext.Request.RequestContext);
             path = urlHelper.ItemDisplayUrl(quantityProduct.Product);
         }
         if (!path.StartsWith(DiscountPart.Pattern, StringComparison.OrdinalIgnoreCase))
             return false;
     }
     if (DiscountPart.Roles.Any()) {
         var user = _wca.GetContext().CurrentUser;
         if (user.Has<UserRolesPart>()) {
             var roles = user.As<UserRolesPart>().Roles;
             if (!roles.Any(r => DiscountPart.Roles.Contains(r))) return false;
         }
     }
     return true;
 }
        public IEnumerable<ShoppingCartQuantityProduct> GetModifiedPrices(
            ShoppingCartQuantityProduct quantityProduct,
            IEnumerable<ShoppingCartQuantityProduct> cartProducts) {

            var discounts = GetPromotions().Cast<Discount>();
            var cartProductList = cartProducts == null ? null : cartProducts.ToList();
            foreach (var discount in discounts) {
                discount.DiscountPart.DisplayUrlResolver = DisplayUrlResolver;
                // Does the discount apply?
                if (!discount.Applies(quantityProduct, cartProductList)) continue;
                // Discount applies
                yield return discount.Apply(quantityProduct, cartProductList);
            }
        }
Example #3
0
        public ShoppingCartQuantityProduct GetDiscountedPrice(
            ShoppingCartQuantityProduct productQuantity,
            IEnumerable<ShoppingCartQuantityProduct> shoppingCartQuantities = null) {

            var modifiedPrices = _priceProviders
                .SelectMany(pp => pp.GetModifiedPrices(productQuantity, shoppingCartQuantities))
                .ToList();
            if (!modifiedPrices.Any()) return productQuantity;
            var result = new ShoppingCartQuantityProduct(productQuantity.Quantity, productQuantity.Product);
            var minPrice = modifiedPrices.Min(mp => mp.Price);
            result.Price = minPrice;
            var lowestPrice = modifiedPrices.FirstOrDefault(mp => Math.Abs(mp.Price - minPrice) < Epsilon);
            if (lowestPrice != null) {
                result.Comment = lowestPrice.Comment;
            }
            return result;
        }
Example #4
0
 public ShoppingCartQuantityProduct Apply(ShoppingCartQuantityProduct quantityProduct, IEnumerable<ShoppingCartQuantityProduct> cartProducts) {
     if (DiscountPart == null) return quantityProduct;
     var comment = DiscountPart.Comment; // TODO: tokenize this
     var percent = DiscountPart.DiscountPercent;
     if (percent != null) {
         return new ShoppingCartQuantityProduct(quantityProduct.Quantity, quantityProduct.Product) {
             Comment = comment,
             Price = Math.Round(quantityProduct.Price * (1 - ((double)percent / 100)), 2)
         };
     }
     var discount = DiscountPart.Discount;
     if (discount != null) {
         return new ShoppingCartQuantityProduct(quantityProduct.Quantity, quantityProduct.Product) {
             Comment = comment,
             Price = Math.Round(Math.Max(0, quantityProduct.Price - (double)discount), 2)
         };
     }
     return quantityProduct;
 }