示例#1
0
 public decimal GetOriginalPrice(ShoppingCartQuantityProduct productQuantity)
 {
     return(ProductPriceService.GetPrice(
                productQuantity.Product,
                ShoppingCart?.Country,
                ShoppingCart?.ZipCode));
 }
示例#2
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);
        }
示例#3
0
        public ShoppingCartQuantityProduct GetDiscount(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, productQuantity.AttributeIdsToValues);
            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;
                result.Promotion = lowestPrice.Promotion;
            }
            result.OriginalPrice = productQuantity.Price;
            return(result);
        }
示例#4
0
 public decimal GetLinePriceAdjustement(ShoppingCartQuantityProduct productQuantity)
 {
     return(ProductPriceService.GetPrice(
                productQuantity.Product,
                productQuantity.LinePriceAdjustment,
                ShoppingCart?.Country,
                ShoppingCart?.ZipCode));
 }
示例#5
0
        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);
        }
示例#6
0
        public ShoppingCartQuantityProduct GetTieredPrice(ShoppingCartQuantityProduct quantityProduct)
        {
            var priceTiers = GetPriceTiers(quantityProduct.Product);
            var priceTier  = priceTiers != null?priceTiers
                             .Where(t => t.Quantity <= quantityProduct.Quantity)
                             .OrderByDescending(t => t.Quantity).Take(1).SingleOrDefault() : null;

            if (priceTier != null)
            {
                quantityProduct.Price = (double)priceTier.Price;
            }
            return(quantityProduct);
        }
示例#7
0
 public IEnumerable <ShoppingCartQuantityProduct> GetModifiedPrices(
     ShoppingCartQuantityProduct quantityProduct,
     IEnumerable <ShoppingCartQuantityProduct> cartProducts)
 {
     if (quantityProduct.Product.DiscountPrice >= 0 &&
         Math.Abs(quantityProduct.Product.Price - quantityProduct.Product.DiscountPrice) > Epsilon)
     {
         yield return(new ShoppingCartQuantityProduct(
                          quantityProduct.Quantity,
                          quantityProduct.Product,
                          quantityProduct.AttributeIdsToValues)
         {
             Price = quantityProduct.Product.DiscountPrice
         });
     }
 }
示例#8
0
        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));
            }
        }
示例#9
0
        public ShoppingCartQuantityProduct GetDiscountedPrice(
            ShoppingCartQuantityProduct productQuantity,
            IEnumerable <ShoppingCartQuantityProduct> shoppingCartQuantities = null)
        {
            // If tiered pricing is enabled, get the tiered price before applying discount
            if (_tieredPriceProvider != null)
            {
                productQuantity = _tieredPriceProvider.GetTieredPrice(productQuantity);
            }

            var discountedProductQuantity = GetDiscount(productQuantity, shoppingCartQuantities);

            // Adjust price based on attributes selected
            if (discountedProductQuantity.AttributeIdsToValues != null)
            {
                foreach (var attr in discountedProductQuantity.AttributeIdsToValues)
                {
                    var value = _attributeService.GetAttributes(new [] { attr.Key }).Single()
                                .AttributeValues.FirstOrDefault(v => v.Text.Trim() == attr.Value.Value.Trim());
                    if (value == null)
                    {
                        // If the attribute doesn't exist, remove the product
                        return(new ShoppingCartQuantityProduct(0, productQuantity.Product, productQuantity.AttributeIdsToValues));
                    }
                    // If the adjustment is to the line, specify, otherwise adjust the per unit price
                    if (value.IsLineAdjustment)
                    {
                        discountedProductQuantity.LinePriceAdjustment += value.PriceAdjustment;
                    }
                    else
                    {
                        discountedProductQuantity.Price += value.PriceAdjustment;
                    }
                }
            }

            return(discountedProductQuantity);
        }
示例#10
0
        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.IsNullOrWhiteSpace(DiscountPart.ExclusionPattern))
            {
                string path = null;
                if (DiscountPart.DisplayUrlResolver != null)
                {
                    path = DiscountPart.DisplayUrlResolver(quantityProduct.Product);
                }
                else if (_wca.GetContext().HttpContext != null)
                {
                    var urlHelper = new UrlHelper(_wca.GetContext().HttpContext.Request.RequestContext);
                    path = urlHelper.ItemDisplayUrl(quantityProduct.Product);
                }
                else
                {
                    var autoroutePart = quantityProduct.Product.As <AutoroutePart>();
                    if (autoroutePart != null)
                    {
                        path = "/" + autoroutePart.Path; // Discount patterns have leading slash
                    }
                }
                if (path == null)
                {
                    return(false);
                }
                if (!string.IsNullOrWhiteSpace(DiscountPart.Pattern))
                {
                    var patternExpression = new Regex(DiscountPart.Pattern, RegexOptions.Singleline | RegexOptions.IgnoreCase);
                    if (!patternExpression.IsMatch(path))
                    {
                        return(false);
                    }
                }
                if (!string.IsNullOrWhiteSpace(DiscountPart.ExclusionPattern))
                {
                    var exclusionPatternExpression = new Regex(DiscountPart.ExclusionPattern,
                                                               RegexOptions.Singleline | RegexOptions.IgnoreCase);
                    if (exclusionPatternExpression.IsMatch(path))
                    {
                        return(false);
                    }
                }
            }
            if (DiscountPart.Roles.Any())
            {
                var user = _wca.GetContext().CurrentUser;
                if (!user.Has <IUserRoles>())
                {
                    return(false);
                }
                var roles = user.As <IUserRoles>().Roles;
                if (!roles.Any(r => DiscountPart.Roles.Contains(r)))
                {
                    return(false);
                }
            }

            return(true);
        }