예제 #1
0
        public void Execute(IRuleExecutionContext context)
        {
            var commerceContext = context.Fact <CommerceContext>();

            //Get configuration
            string  specificCategory     = Hc_SpecificCategory.Yield(context);
            decimal itemsToAward         = Hc_ItemsToAward.Yield(context);
            decimal itemsToPurchase      = Hc_ItemsToPurchase.Yield(context);
            bool    includeSubCategories = Hc_IncludeSubCategories.Yield(context);
            decimal amountOff            = Hc_AmountOff.Yield(context);
            string  applyActionTo        = Hc_ApplyActionTo.Yield(context);
            int     actionLimit          = Hc_ActionLimit.Yield(context);

            if (string.IsNullOrEmpty(specificCategory) ||
                itemsToAward == 0 ||
                itemsToPurchase == 0 ||
                amountOff == 0 ||
                string.IsNullOrEmpty(applyActionTo) ||
                actionLimit == 0)
            {
                return;
            }

            //Get data
            IEnumerable <CartLineComponent> categoryLines =
                categoryCartLinesResolver.Resolve(commerceContext, specificCategory, includeSubCategories);

            if (categoryLines == null)
            {
                return;
            }

            //Validate and apply action
            int     cartQuantity        = Convert.ToInt32(categoryLines.Sum(x => x.Quantity));
            decimal cartProductsToAward = Math.Floor(cartQuantity / itemsToPurchase) * itemsToAward;

            decimal productsToAward = cartProductsToAward > actionLimit ? actionLimit : cartProductsToAward;

            if (productsToAward <= 0)
            {
                return;
            }

            var discountApplicator = new DiscountApplicator(commerceContext);

            discountApplicator.ApplyPriceDiscount(categoryLines, amountOff, new DiscountOptions
            {
                ActionLimit      = Convert.ToInt32(productsToAward),
                ApplicationOrder = ApplicationOrder.Parse(applyActionTo),
                AwardingBlock    = nameof(CartEveryXItemsInCategoryPriceDiscountAction)
            });
        }
        public void Execute(IRuleExecutionContext context)
        {
            var commerceContext = context.Fact <CommerceContext>();

            //Get configuration
            string  specificCategory     = Hc_SpecificCategory.Yield(context);
            decimal specificValue        = Hc_SpecificValue.Yield(context);
            bool    includeSubCategories = Hc_IncludeSubCategories.Yield(context);
            decimal amountOff            = Hc_AmountOff.Yield(context);
            string  applyActionTo        = Hc_ApplyActionTo.Yield(context);
            int     actionLimit          = Hc_ActionLimit.Yield(context);

            if (string.IsNullOrEmpty(specificCategory) ||
                specificValue == 0 ||
                amountOff == 0 ||
                string.IsNullOrEmpty(applyActionTo) ||
                actionLimit == 0 ||
                Hc_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 (!Hc_Operator.Evaluate(productAmount, specificValue))
            {
                return;
            }

            var discountApplicator = new DiscountApplicator(commerceContext);

            discountApplicator.ApplyPriceDiscount(categoryLines, amountOff, new DiscountOptions
            {
                ActionLimit      = actionLimit,
                ApplicationOrder = ApplicationOrder.Parse(applyActionTo),
                AwardingBlock    = nameof(CartItemsMatchingInCategoryPriceDiscountAction)
            });
        }