Exemple #1
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);
        }
Exemple #2
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);
        }