Пример #1
0
        private CartLineComponent CreateLine(Cart cart, CommerceContext commerceContext, string targetItemId, decimal quantity)
        {
            SellableItem gift = AsyncHelper.RunSync(() => _getCommand.Process(commerceContext, targetItemId, false));

            if (gift == null)
            {
                return(null);
            }

            // To make sure all pipeline blocks are executed and do not influence the current cart, we add
            // the gift line to a temporary cart and then copy it to the current cart.
            var temporaryCart = cart.Clone <Cart>();

            temporaryCart.AddComponents(new TemporaryCartComponent(cart.Id));

            var freeGift = new CartLineComponent
            {
                ItemId   = targetItemId,
                Quantity = quantity
            };

            temporaryCart = AsyncHelper.RunSync(() => _addCommand.Process(commerceContext, temporaryCart, freeGift));

            CartLineComponent line = temporaryCart.Lines.Single(x => x.ItemId == targetItemId);

            if (gift.ListPrice.Amount > 0)
            {
                decimal discount = new MoneyEx(commerceContext, gift.ListPrice).Round().Value.Amount;

                line.Adjustments.Add(AwardedAdjustmentFactory.CreateLineLevelAwardedAdjustment(discount * -1,
                                                                                               nameof(CartFreeGiftAction), line.Id, commerceContext));
            }

            return(line);
        }
Пример #2
0
        public void Execute(IRuleExecutionContext context)
        {
            //Get configuration
            var commerceContext = context.Fact <CommerceContext>();
            var cart            = commerceContext?.GetObject <Cart>();

            if (cart == null || !cart.Lines.Any() || !cart.HasComponent <FulfillmentComponent>())
            {
                return;
            }

            decimal amountOff = Hc_SpecificAmount.Yield(context);

            if (amountOff == 0)
            {
                return;
            }

            //Get data
            decimal fulfillmentFee = GetFulfillmentFee(cart);

            if (fulfillmentFee == 0)
            {
                return;
            }

            if (amountOff > fulfillmentFee)
            {
                amountOff = fulfillmentFee;
            }

            //Apply action
            amountOff = new MoneyEx(commerceContext, amountOff).Round().Value.Amount;

            CartLevelAwardedAdjustment adjustment = AwardedAdjustmentFactory.CreateCartLevelAwardedAdjustment(amountOff * -1,
                                                                                                              nameof(CartAmountOffFulfillmentAction), commerceContext);

            cart.Adjustments.Add(adjustment);

            cart.GetComponent <MessagesComponent>()
            .AddPromotionApplied(commerceContext, nameof(CartAmountOffFulfillmentAction));
        }