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 }; var rewards = await _marketingService.EvaluatePromotionRewardsAsync(promotionContext); var validRewards = rewards.Where(r => r.RewardType == PromotionRewardType.CatalogItemAmountReward && r.IsValid); foreach (var price in prices) { var validReward = validRewards.FirstOrDefault(r => r.ProductId == price.ProductId); if (validReward != null) { price.ActiveDiscount = validReward.ToDiscountWebModel(price.SalePrice.Amount, 1, price.Currency); } } return(Json(prices, JsonRequestBehavior.AllowGet)); }
private async Task EvaluatePromotionsAsync() { _cart.Discounts.Clear(); foreach (var lineItem in _cart.Items) { lineItem.Discounts.Clear(); } foreach (var shipment in _cart.Shipments) { shipment.Discounts.Clear(); } var promotionItems = _cart.Items.Select(i => i.ToPromotionItem()).ToList(); var promotionContext = new PromotionEvaluationContext { CartPromoEntries = promotionItems, CartTotal = _cart.Total, Coupon = _cart.Coupon != null ? _cart.Coupon.Code : null, Currency = _cart.Currency, CustomerId = _customer.Id, IsRegisteredUser = _customer.HasAccount, PromoEntries = promotionItems, StoreId = _store.Id, }; var rewards = await _marketingService.EvaluatePromotionRewardsAsync(promotionContext); var cartSubtotalRewards = rewards.Where(r => r.RewardType == PromotionRewardType.CartSubtotalReward); foreach (var cartSubtotalReward in cartSubtotalRewards) { var discount = cartSubtotalReward.ToDiscountWebModel(_cart.SubTotal.Amount, 1, _cart.Currency); if (cartSubtotalReward.IsValid) { _cart.Discounts.Add(discount); } if (_cart.Coupon != null && !string.IsNullOrEmpty(_cart.Coupon.Code)) { _cart.Coupon.Amount = discount.Amount; _cart.Coupon.AppliedSuccessfully = cartSubtotalReward.IsValid; _cart.Coupon.Description = cartSubtotalReward.Promotion.Description; _cart.Coupon.ErrorCode = !cartSubtotalReward.IsValid ? "InvalidCouponCode" : null; } } var catalogItemRewards = rewards.Where(r => r.RewardType == PromotionRewardType.CatalogItemAmountReward); foreach (var catalogItemReward in catalogItemRewards) { if (catalogItemReward.IsValid) { var lineItem = _cart.Items.FirstOrDefault(i => i.ProductId == catalogItemReward.ProductId); if (lineItem != null) { var discount = catalogItemReward.ToDiscountWebModel(lineItem.SalePrice.Amount, lineItem.Quantity, lineItem.Currency); lineItem.Discounts.Add(discount); lineItem.PlacedPrice = lineItem.SalePrice - discount.Amount / lineItem.Quantity; lineItem.ExtendedPrice = lineItem.PlacedPrice * lineItem.Quantity; } } } var shippingRewards = rewards.Where(r => r.RewardType == PromotionRewardType.ShipmentReward); foreach (var shippingReward in shippingRewards) { if (shippingReward.IsValid) { var shipment = _cart.Shipments.FirstOrDefault(); if (shipment != null) { shipment.Discounts.Add(shippingReward.ToDiscountWebModel(shipment.ShippingPrice.Amount, 1, shipment.Currency)); } } } CalculateTotals(); }