public static PromotionEvaluationContext ToPromotionEvaluationContext(this WorkContext workContext) { var retVal = new PromotionEvaluationContext { CartPromoEntries = workContext.CurrentCart.Items.Select(x => x.ToPromotionItem()).ToList(), CartTotal = workContext.CurrentCart.Total, Coupon = workContext.CurrentCart.Coupon != null ? workContext.CurrentCart.Coupon.Code : null, Currency = workContext.CurrentCurrency, CustomerId = workContext.CurrentCustomer.Id, IsRegisteredUser = workContext.CurrentCustomer.IsRegisteredUser, Language = workContext.CurrentLanguage, StoreId = workContext.CurrentStore.Id }; //Set cart lineitems as default promo items retVal.PromoEntries = retVal.CartPromoEntries; if (workContext.CurrentProduct != null) { retVal.PromoEntry = workContext.CurrentProduct.ToPromotionItem(); } if(workContext.CurrentCatalogSearchResult != null && workContext.CurrentCatalogSearchResult.Products != null ) { retVal.PromoEntries = workContext.CurrentCatalogSearchResult.Products.Select(x => x.ToPromotionItem()).ToList(); } return retVal; }
public async Task<ActionResult> GetActualProductPricesJson(Product[] products) { var prices = new List<ProductPrice>(); if (products == null) { return Json(prices, JsonRequestBehavior.AllowGet); } var pricesResponse = await _pricingApi.PricingModuleEvaluatePricesAsync( evalContextProductIds: products.Select(p => p.Id).ToList(), evalContextCatalogId: WorkContext.CurrentStore.Catalog, evalContextCurrency: WorkContext.CurrentCurrency.Code, evalContextCustomerId: WorkContext.CurrentCustomer.Id, evalContextLanguage: WorkContext.CurrentLanguage.CultureName, evalContextStoreId: WorkContext.CurrentStore.Id); if (pricesResponse == null) { return Json(prices, JsonRequestBehavior.AllowGet); } prices = pricesResponse.Select(p => p.ToWebModel()).ToList(); var promotionContext = new PromotionEvaluationContext { CartPromoEntries = GetCartPromoEntries(WorkContext.CurrentCart), Currency = WorkContext.CurrentCurrency, CustomerId = WorkContext.CurrentCustomer.Id, IsRegisteredUser = WorkContext.CurrentCustomer.HasAccount, Language = WorkContext.CurrentLanguage, PromoEntries = GetPromoEntries(products, prices), StoreId = WorkContext.CurrentStore.Id }; foreach (var product in products) { product.Currency = WorkContext.CurrentCurrency; product.Price = prices.FirstOrDefault(p => p.ProductId == product.Id); } await _promotionEvaluator.EvaluateDiscountsAsync(promotionContext, products); prices = products.Select(p => p.Price).ToList(); foreach (var price in prices) { if (price.ActiveDiscount != null) { price.AbsoluteBenefit += price.ActiveDiscount.Amount; price.ActualPrice = price.SalePrice - price.AbsoluteBenefit; } } return Json(prices, JsonRequestBehavior.AllowGet); }
public async Task EvaluateDiscountsAsync(PromotionEvaluationContext context, IEnumerable<IDiscountable> owners) { var rewards = await _marketingApi.MarketingModulePromotionEvaluatePromotionsAsync(context.ToServiceModel()); if (rewards == null) { return; } foreach (var owner in owners) { owner.ApplyRewards(rewards.Select(r => r.ToWebModel(owner.Currency))); } }
private async Task EvaluatePromotionsAsync() { var promotionItems = _cart.Items.Select(i => i.ToPromotionItem()).ToList(); var promotionContext = new PromotionEvaluationContext(); promotionContext.CartPromoEntries = promotionItems; promotionContext.CartTotal = _cart.Total; promotionContext.Coupon = _cart.Coupon != null ? _cart.Coupon.Code : null; promotionContext.Currency = _cart.Currency; promotionContext.CustomerId = _customer.Id; promotionContext.IsRegisteredUser = _customer.HasAccount; promotionContext.Language = _language; promotionContext.PromoEntries = promotionItems; promotionContext.StoreId = _store.Id; await _promotionEvaluator.EvaluateDiscountsAsync(promotionContext, new IDiscountable[] { _cart }); CalculateTotals(); }
public void EvaluateDiscounts(PromotionEvaluationContext context, IEnumerable<IDiscountable> owners) { var rewards = _marketingApi.MarketingModulePromotionEvaluatePromotions(context.ToServiceModel()); InnerEvaluateDiscounts(rewards, owners); }
public async Task EvaluateDiscountsAsync(PromotionEvaluationContext context, IEnumerable<IDiscountable> owners) { var rewards = await _marketingApi.MarketingModulePromotionEvaluatePromotionsAsync(context.ToServiceModel()); InnerEvaluateDiscounts(rewards, owners); }