예제 #1
0
        public virtual async Task <WishList> Process(CommerceContext commerceContext, string wishListId, bool secureResult = true)
        {
            using (CommandActivity.Start(commerceContext, this))
            {
                var context = commerceContext.GetPipelineContextOptions();
                var resolveWishListArgument = new ResolveWishListArgument(commerceContext.CurrentShopName(), wishListId, commerceContext.CurrentShopperId());
                var objects = commerceContext.GetObjects <WishList>();
                if (objects.Any(p => p.Id == wishListId))
                {
                    commerceContext.Logger.LogTrace(string.Format("GetWishListCommand.AlreadyLoaded: WishListId={0}", wishListId), Array.Empty <object>());
                    return(objects.FirstOrDefault(p => p.Id == wishListId));
                }
                commerceContext.Logger.LogTrace(string.Format("GetWishListCommand.LoadingWishList: WishListId={0}", wishListId), Array.Empty <object>());
                var wishList = await _pipeline.Run(resolveWishListArgument, context);

                commerceContext.Logger.LogTrace(string.Format("GetWishListCommand.WishListLoaded: WishListId={0}", wishListId), Array.Empty <object>());

                return(wishList);
            }
        }
        public void Execute(IRuleExecutionContext context)
        {
            CommerceContext commerceContext  = context.Fact <CommerceContext>((string)null);
            CommerceContext commerceContext1 = commerceContext;
            Cart            cart             = commerceContext1 != null?commerceContext1.GetObjects <Cart>().FirstOrDefault <Cart>() : (Cart)null;

            CommerceContext commerceContext2 = commerceContext;
            CartTotals      totals           = commerceContext2 != null?commerceContext2.GetObjects <CartTotals>().FirstOrDefault <CartTotals>() : (CartTotals)null;

            if (cart == null || !cart.Lines.Any <CartLineComponent>() || (totals == null || !totals.Lines.Any <KeyValuePair <string, Totals> >()))
            {
                return;
            }

            List <CartLineComponent> list = new List <CartLineComponent>();

            //Get the valid cartline items that matches specific tag provided
            foreach (var cartLine in cart.Lines.Where(x => x.HasComponent <CartProductComponent>()))
            {
                var firstOrDefault = cartLine.GetComponent <CartProductComponent>().Tags.FirstOrDefault(t => t.Name == this.Tag.Yield(context));
                if (!string.IsNullOrEmpty(firstOrDefault?.Name))
                {
                    list.Add(cartLine);
                }
            }
            if (!list.Any <CartLineComponent>())
            {
                return;
            }

            list.ForEach((Action <CartLineComponent>)(line =>
            {
                if (!totals.Lines.ContainsKey(line.Id))
                {
                    return;
                }
                PropertiesModel propertiesModel = commerceContext.GetObject <PropertiesModel>();
                string discountPolicy           = commerceContext.GetPolicy <KnownCartAdjustmentTypesPolicy>().Discount;

                //Extract the coupon code in-order to associate coupon code with adjustment (1 to 1 mapping)
                string couponCode = string.Empty;
                if (cart.HasComponent <CartCouponsComponent>())
                {
                    if (cart.GetComponent <CartCouponsComponent>().List != null && cart.GetComponent <CartCouponsComponent>().List.Any())
                    {
                        couponCode = cart.GetComponent <CartCouponsComponent>().List.FirstOrDefault(c => c.Promotion.EntityTarget == (string)propertiesModel?.GetPropertyValue("PromotionId"))?.CouponId;
                    }
                }

                //calculate discounts on specified custom pricing
                if (line.HasComponent <CustomPriceInfoComponent>())
                {
                    var customPriceInfo = line.GetComponent <CustomPriceInfoComponent>();
                    Decimal discount    = 0;
                    if (this.BasePrice.Yield(context))
                    {
                        discount += (Decimal)((double)this.PercentOff.Yield(context) * 0.01) * (decimal.Parse(customPriceInfo.BasePrice.ToString()));
                    }
                    if (this.ActivationPrice.Yield(context))
                    {
                        discount += (Decimal)((double)this.PercentOff.Yield(context) * 0.01) * (decimal.Parse(customPriceInfo.ActivationPrice.ToString()));
                    }
                    if (this.DeliveryPrice.Yield(context))
                    {
                        discount += (Decimal)((double)this.PercentOff.Yield(context) * 0.01) * (decimal.Parse(customPriceInfo.DeliveryPrice.ToString()));
                    }
                    if (commerceContext.GetPolicy <GlobalPricingPolicy>().ShouldRoundPriceCalc)
                    {
                        discount = Decimal.Round(discount, commerceContext.GetPolicy <GlobalPricingPolicy>().RoundDigits, commerceContext.GetPolicy <GlobalPricingPolicy>().MidPointRoundUp ? MidpointRounding.AwayFromZero : MidpointRounding.ToEven);
                    }
                    Decimal amount = discount * Decimal.MinusOne;

                    //Create an adjustment and associate coupon code with coupon description delimited by '|' [Workaround for known issue with Promotion Plugin]
                    IList <AwardedAdjustment> adjustments = line.Adjustments;
                    adjustments.Add((AwardedAdjustment) new CartLineLevelAwardedAdjustment()
                    {
                        Name           = (propertiesModel?.GetPropertyValue("PromotionText") as string ?? discountPolicy),
                        DisplayName    = couponCode + "|" + (propertiesModel?.GetPropertyValue("PromotionCartText") as string ?? discountPolicy),
                        Adjustment     = new Money(commerceContext.CurrentCurrency(), amount),
                        AdjustmentType = discountPolicy,
                        IsTaxable      = false,
                        AwardingBlock  = nameof(CustomPercentOffAction)
                    });
                    totals.Lines[line.Id].SubTotal.Amount = totals.Lines[line.Id].SubTotal.Amount + amount;
                    line.GetComponent <MessagesComponent>().AddMessage(commerceContext.GetPolicy <KnownMessageCodePolicy>().Promotions, string.Format("PromotionApplied: {0}", propertiesModel?.GetPropertyValue("PromotionId") ?? (object)nameof(CustomPercentOffAction)));
                }
            }));
        }