public bool Evaluate(IRuleExecutionContext context) { DateTime date = Ny_Date.Yield(context).DateTime; List <Order> orders = AsyncHelper.RunSync(() => orderResolver.Resolve(context.Fact <CommerceContext>())); if (orders == null) { return(false); } DateTime firstPurchaseDate = orders.Min(x => x.OrderPlacedDate).DateTime; return(Ny_Operator.Evaluate(firstPurchaseDate, date)); }
public void Execute(IRuleExecutionContext context) { var commerceContext = context.Fact <CommerceContext>(); //Get configuration string specificCategory = Ny_SpecificCategory.Yield(context); decimal specificValue = Ny_SpecificValue.Yield(context); bool includeSubCategories = Ny_IncludeSubCategories.Yield(context); decimal percentageOff = Ny_PercentageOff.Yield(context); string applyActionTo = Ny_ApplyActionTo.Yield(context); int actionLimit = Ny_ActionLimit.Yield(context); if (string.IsNullOrEmpty(specificCategory) || specificValue == 0 || percentageOff == 0 || string.IsNullOrEmpty(applyActionTo) || actionLimit == 0 || Ny_Operator == null) { return; } //Get data IEnumerable <CartLineComponent> categoryLines = categoryCartLinesResolver.Resolve(commerceContext, specificCategory, includeSubCategories); if (categoryLines == null) { return; } //Validate and apply action decimal productAmount = categoryLines.Sum(x => x.Quantity); if (!Ny_Operator.Evaluate(productAmount, specificValue)) { return; } var discountApplicator = new DiscountApplicator(commerceContext); discountApplicator.ApplyPercentageDiscount(categoryLines, percentageOff, new DiscountOptions { ActionLimit = actionLimit, ApplicationOrder = ApplicationOrder.Parse(applyActionTo), AwardingBlock = nameof(CartItemsMatchingInCategoryPercentageDiscountAction) }); }