Beispiel #1
0
        public async Task EvaluateProductPricesAsync(IEnumerable <Product> products)
        {
            var workContext = _workContextFactory();
            //Evaluate products prices
            var evalContext = products.ToServiceModel(workContext);

            var pricesResponse = await _pricingApi.PricingModuleEvaluatePricesAsync(evalContext);

            ApplyProductPricesInternal(products, pricesResponse);
        }
Beispiel #2
0
        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 <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));
        }
Beispiel #4
0
        public async Task EvaluateProductPricesAsync(IEnumerable <Product> products)
        {
            var workContext = _workContextFactory();

            //Evaluate products prices
            var evalContext = new VirtoCommerceDomainPricingModelPriceEvaluationContext
            {
                ProductIds   = products.Select(p => p.Id).ToList(),
                PricelistIds = workContext.CurrentPricelists.Select(p => p.Id).ToList(),
                CatalogId    = workContext.CurrentStore.Catalog,
                CustomerId   = workContext.CurrentCustomer.Id,
                Language     = workContext.CurrentLanguage.CultureName,
                CertainDate  = workContext.StorefrontUtcNow,
                StoreId      = workContext.CurrentStore.Id
            };

            var pricesResponse = await _pricingApi.PricingModuleEvaluatePricesAsync(evalContext);

            var alreadyDefinedProductsPriceGroups = pricesResponse.Select(x => x.ToWebModel(workContext.AllCurrencies, workContext.CurrentLanguage)).GroupBy(x => x.ProductId);

            foreach (var product in products)
            {
                var productPricesGroup = alreadyDefinedProductsPriceGroups.FirstOrDefault(x => x.Key == product.Id);
                if (productPricesGroup != null)
                {
                    //Get first price for each currency
                    product.Prices = productPricesGroup.GroupBy(x => x.Currency).Select(x => x.FirstOrDefault()).Where(x => x != null).ToList();
                }
                //Need add product price for all store currencies (even if not returned from api need make it by currency exchange convertation)
                foreach (var storeCurrency in workContext.CurrentStore.Currencies)
                {
                    var price = product.Prices.FirstOrDefault(x => x.Currency == storeCurrency);
                    if (price == null)
                    {
                        price = new ProductPrice(storeCurrency);
                        if (product.Prices.Any())
                        {
                            price = product.Prices.First().ConvertTo(storeCurrency);
                        }
                        product.Prices.Add(price);
                    }
                }
                product.Currency = workContext.CurrentCurrency;
                product.Price    = product.Prices.FirstOrDefault(x => x.Currency.Equals(workContext.CurrentCurrency));
            }
        }
Beispiel #5
0
        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 = WorkContext.ToPromotionEvaluationContext();

            promotionContext.PromoEntries = GetPromoEntries(products, prices);

            foreach (var product in products)
            {
                product.Currency = WorkContext.CurrentCurrency;
                product.Price    = prices.FirstOrDefault(p => p.ProductId == product.Id);
            }
            await _promotionEvaluator.EvaluateDiscountsAsync(promotionContext, products);

            return(Json(prices, JsonRequestBehavior.AllowGet));
        }