Ejemplo n.º 1
0
        public void ApplyCriteria(CouponApplicabilityCriterionContext context,
                                  // Use outerCriterion to negate the test, so we can easily do
                                  // true/false
                                  Func <bool, bool> outerCriterion,
                                  LocalizedString failureMessage)
        {
            if (context.IsApplicable)
            {
                var allowedTypes = ProductTypes(context)
                                   .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                                   .Select(s => s.Trim());
                var result = outerCriterion(
                    // the "base" test is "contains products of type". If
                    // outerCriterion is b => !b that turns in
                    // "doesn't contain products of type"
                    context.ApplicabilityContext
                    .ShoppingCart
                    .GetProducts()
                    .Any(pq => allowedTypes.Contains(pq.Product.TypeDefinition.Name)));

                if (!result)
                {
                    context.ApplicabilityContext.Message = failureMessage;
                }
                context.IsApplicable = result;
                context.ApplicabilityContext.IsApplicable = result;
            }
        }
Ejemplo n.º 2
0
        private void ApplyCriterion(CouponApplicabilityCriterionContext context)
        {
            if (context.IsApplicable)
            {
                var formState = context.State;

                var op = (DateTimeOperator)Enum.Parse(typeof(DateTimeOperator), Convert.ToString(formState.Operator));

                var currentCulture = CultureInfo.CurrentCulture;

                string dateFrom          = Convert.ToString(formState.DateFrom);
                string timeFrom          = Convert.ToString(formState.TimeFrom);
                var    cultureFrom       = CultureInfo.GetCultureInfo(formState.CultureFrom.Value);
                var    dateReferenceFrom = Convert.ToDateTime(dateFrom + " " + timeFrom, cultureFrom).ToUniversalTime();

                string dateTo          = Convert.ToString(formState.DateTo);
                string timeTo          = Convert.ToString(formState.TimeTo);
                var    cultureTo       = CultureInfo.GetCultureInfo(formState.CultureTo.Value);
                var    dateReferenceTo = Convert.ToDateTime(dateTo + " " + timeTo, cultureTo).ToUniversalTime();

                var utcNow = DateTime.UtcNow;

                switch (op)
                {
                case DateTimeOperator.LessThan:
                    if (utcNow > dateReferenceTo)
                    {
                        context.IsApplicable = false;
                        context.ApplicabilityContext.IsApplicable = false;
                    }
                    break;

                case DateTimeOperator.GreaterThan:
                    if (utcNow < dateReferenceFrom)
                    {
                        context.IsApplicable = false;
                        context.ApplicabilityContext.IsApplicable = false;
                    }
                    break;

                case DateTimeOperator.Between:
                    if (utcNow < dateReferenceFrom ||
                        utcNow > dateReferenceTo)
                    {
                        context.IsApplicable = false;
                        context.ApplicabilityContext.IsApplicable = false;
                    }
                    break;

                default:
                    // Invalid operator.
                    context.IsApplicable = false;
                    context.ApplicabilityContext.IsApplicable = false;
                    break;
                }
            }
        }
        private void ApplyCriteria(CouponApplicabilityCriterionContext context)
        {
            if (context.IsApplicable)
            {
                var result = CookieValueForm.GetFilterPredicate(context.State)(_httpContextAccessor.Current().Request);

                if (!result)
                {
                    context.ApplicabilityContext.Message =
                        T("Coupon code {0} is not valid", context.CouponRecord.Code);
                }
                context.IsApplicable = result;
                context.ApplicabilityContext.IsApplicable = result;
            }
        }
        public void ApplyCriteria(CouponApplicabilityCriterionContext context)
        {
            var result = false;

            if (context.IsApplicable)
            {
                // get product in shopping cart
                var products = context
                               .ApplicabilityContext
                               .ShoppingCart
                               .GetProducts();

                // to apply the coupon, no products in the cart must be discounted
                result = !products.Any(p => HasDiscount(p.Product));

                context.ApplicabilityContext.IsApplicable = result;
                context.IsApplicable = result;
            }
        }
Ejemplo n.º 5
0
        public void ApplyCriterion(CouponApplicabilityCriterionContext context,
                                   // Use outerCriterion to negate the test, so we can easily do
                                   // contains / doesn't contain
                                   Func <bool, bool> outerCriterion)
        {
            if (context.IsApplicable)
            {
                // If there is no configured term, this delegate will not affect the
                // applicability for the coupon in any way.
                // get from state the comma separated list of term ids
                List <int> allTermIds  = new List <int>();
                var        termsString = (string)context.State.Terms;
                if (!string.IsNullOrWhiteSpace(termsString))
                {
                    allTermIds = termsString
                                 .Split(new char[] { ',' })
                                 .Select(int.Parse)
                                 .ToList();
                }
                string selectedMultipleTerms = Convert.ToString(context.State.TermIds);
                if (!string.IsNullOrEmpty(selectedMultipleTerms))
                {
                    allTermIds = allTermIds.Union(selectedMultipleTerms
                                                  .Split(new[] { ',' })
                                                  .Select(Int32.Parse)
                                                  .ToList()).ToList();
                }

                // check the list of ids for taxonomies
                // if there are, select all term ids
                allTermIds = allTermIds.Union(allTermIds
                                              .Where(x => _taxonomyService.GetTaxonomy(x) != null)
                                              .SelectMany(t => _taxonomyService.GetTerms(t).Select(term => term.Id))
                                              .ToList()).ToList();

                if (allTermIds.Any())
                {
                    bool.TryParse(context.State.IncludeChildren?.Value, out bool includeChildren);
                    var terms = allTermIds
                                .SelectMany(id => GetTerms(id, includeChildren))
                                .Distinct();
                    // check again, in case the ids do not match any term
                    if (terms.Any())
                    {
                        var result = false;

                        // get product in shopping cart
                        var products = context
                                       .ApplicabilityContext
                                       .ShoppingCart
                                       .GetProducts();

                        // "is one of" or "is all of"
                        int op = Convert.ToInt32(context.State.Operator);

                        var opCart = (SelectTermsOperator)Enum.Parse(typeof(SelectTermsOperator), Convert.ToString(context.State.OperatorCart));

                        List <ShoppingCartQuantityProduct> checkedProductsList = new List <ShoppingCartQuantityProduct>();
                        switch (opCart)
                        {
                        case SelectTermsOperator.AllProducts:
                            checkedProductsList = GetProductByTerm(terms, products, op);
                            if (products.Count() == checkedProductsList.Count())
                            {
                                result = true;
                            }
                            break;

                        case SelectTermsOperator.OneProduct:
                            checkedProductsList = GetProductByTerm(terms, products, op);
                            result = checkedProductsList.Any();
                            break;

                        case SelectTermsOperator.InsideCart:
                            var termsPart = products
                                            .Where(p => p.Product.As <TermsPart>() != null)
                                            .SelectMany(p => p.Product.As <TermsPart>().TermParts);

                            // if no term is selected in the product, we already know this will be false
                            if (termsPart.Any())
                            {
                                var selectedTerms = termsPart.Select(p => p.TermPart);

                                switch (op)
                                {
                                case 0:     // is one of
                                    result = terms.Any(t => selectedTerms.Contains(t));
                                    break;

                                case 1:     // is all of
                                    result = terms.All(t => selectedTerms.Contains(t));
                                    break;
                                }
                            }
                            break;
                        }
                        result = outerCriterion(result);

                        context.ApplicabilityContext.IsApplicable = result;
                        context.IsApplicable = result;
                    }
                }
            }
        }