protected virtual async Task LoadProductDependencies(List <Product> products, ItemResponseGroup responseGroup, WorkContext workContext) { if (!products.IsNullOrEmpty()) { var taskList = new List <Task>(); if (responseGroup.HasFlag(ItemResponseGroup.Inventory)) { taskList.Add(LoadProductInventoriesAsync(products, workContext)); } if (responseGroup.HasFlag(ItemResponseGroup.ItemAssociations)) { taskList.Add(LoadProductsAssociationsAsync(products, workContext)); } if (responseGroup.HasFlag(ItemResponseGroup.ItemWithPrices)) { taskList.Add(_pricingService.EvaluateProductPricesAsync(products, workContext)); } if (responseGroup.HasFlag(ItemResponseGroup.ItemWithVendor)) { taskList.Add(LoadProductVendorsAsync(products, workContext)); } if (workContext.CurrentStore.SubscriptionEnabled && responseGroup.HasFlag(ItemResponseGroup.ItemWithPaymentPlan)) { taskList.Add(LoadProductPaymentPlanAsync(products, workContext)); } await Task.WhenAll(taskList.ToArray()); foreach (var product in products) { product.IsBuyable = new ProductIsBuyableSpecification().IsSatisfiedBy(product); product.IsAvailable = new ProductIsAvailableSpecification(product).IsSatisfiedBy(1); product.IsInStock = new ProductIsInStockSpecification().IsSatisfiedBy(product); } } }
public virtual async Task <Product[]> GetProductsAsync(string[] ids, ItemResponseGroup responseGroup = ItemResponseGroup.None) { Product[] result; if (ids.IsNullOrEmpty()) { result = new Product[0]; } else { var workContext = _workContextFactory(); if (responseGroup == ItemResponseGroup.None) { responseGroup = workContext.CurrentProductResponseGroup; } result = await GetProductsAsync(ids, responseGroup, workContext); var allProducts = result.Concat(result.SelectMany(p => p.Variations)).ToList(); if (!allProducts.IsNullOrEmpty()) { var taskList = new List <Task>(); if (responseGroup.HasFlag(ItemResponseGroup.ItemAssociations)) { taskList.Add(LoadProductAssociationsAsync(allProducts)); } if (responseGroup.HasFlag(ItemResponseGroup.Inventory)) { taskList.Add(LoadProductInventoriesAsync(allProducts, workContext)); } if (responseGroup.HasFlag(ItemResponseGroup.ItemWithPrices)) { taskList.Add(_pricingService.EvaluateProductPricesAsync(allProducts, workContext)); } if (responseGroup.HasFlag(ItemResponseGroup.ItemWithVendor)) { taskList.Add(LoadProductVendorsAsync(allProducts, workContext)); } if (workContext.CurrentStore.SubscriptionEnabled && responseGroup.HasFlag(ItemResponseGroup.ItemWithPaymentPlan)) { taskList.Add(LoadProductPaymentPlanAsync(allProducts, workContext)); } await Task.WhenAll(taskList.ToArray()); foreach (var product in allProducts) { product.IsAvailable = await _productAvailabilityService.IsAvailable(product, 1); } } } return(result); }
public virtual async Task Run(SearchProductResponse parameter, Func <SearchProductResponse, Task> next) { if (parameter == null) { throw new ArgumentNullException(nameof(parameter)); } var query = parameter.Query; if (query == null) { throw new OperationCanceledException("Query must be set"); } // Map indexed prices foreach (var expProducts in parameter.Results) { expProducts.AllPrices = _mapper.Map <IEnumerable <ProductPrice> >(expProducts.IndexedPrices, context => { context.Items["all_currencies"] = parameter.AllStoreCurrencies; }).ToList(); if (parameter.Currency != null) { expProducts.AllPrices = expProducts.AllPrices.Where(x => (x.Currency == null) || x.Currency.Equals(parameter.Currency)).ToList(); } } // If prices evaluation requested var responseGroup = EnumUtility.SafeParse(query.GetResponseGroup(), ExpProductResponseGroup.None); if (responseGroup.HasFlag(ExpProductResponseGroup.LoadPrices)) { //evaluate prices only if product missed prices in the index storage var productsWithoutPrices = parameter.Results.Where(x => !x.IndexedPrices.Any()).ToArray(); if (productsWithoutPrices.Any()) { var evalContext = AbstractTypeFactory <PricingModule.Core.Model.PriceEvaluationContext> .TryCreateInstance(); evalContext.Currency = query.CurrencyCode; evalContext.StoreId = query.StoreId; evalContext.CustomerId = query.UserId; evalContext.Language = query.CultureName; await _pipeline.Execute(evalContext); evalContext.ProductIds = productsWithoutPrices.Select(x => x.Id).ToArray(); var prices = await _pricingService.EvaluateProductPricesAsync(evalContext); foreach (var product in productsWithoutPrices) { product.AllPrices = _mapper.Map <IEnumerable <ProductPrice> >(prices.Where(x => x.ProductId == product.Id), options => { options.Items["all_currencies"] = parameter.AllStoreCurrencies; options.Items["currency"] = parameter.Currency; }).ToList(); } } } await next(parameter); }
public async Task <IActionResult> EvaluatePrices([FromBody] PriceEvaluationContext evalContext) { var retVal = (await _pricingService.EvaluateProductPricesAsync(evalContext)).ToArray(); return(Ok(retVal)); }
public async Task DoExportAsync(Stream outStream, CsvExportInfo exportInfo, Action <ExportImportProgressInfo> progressCallback) { var prodgressInfo = new ExportImportProgressInfo { Description = "loading products..." }; var streamWriter = new StreamWriter(outStream, Encoding.UTF8, 1024, true) { AutoFlush = true }; using (var csvWriter = new CsvWriter(streamWriter)) { //Notification progressCallback(prodgressInfo); //Load all products to export var products = await LoadProducts(exportInfo.CatalogId, exportInfo.CategoryIds, exportInfo.ProductIds); var allProductIds = products.Select(x => x.Id).ToArray(); //Load prices for products prodgressInfo.Description = "loading prices..."; progressCallback(prodgressInfo); var priceEvalContext = new PriceEvaluationContext { ProductIds = allProductIds, PricelistIds = exportInfo.PriceListId == null ? null : new[] { exportInfo.PriceListId }, Currency = exportInfo.Currency }; var allProductPrices = (await _pricingService.EvaluateProductPricesAsync(priceEvalContext)).ToList(); //Load inventories prodgressInfo.Description = "loading inventory information..."; progressCallback(prodgressInfo); var inventorySearchCriteria = new InventorySearchCriteria() { ProductIds = allProductIds, FulfillmentCenterIds = string.IsNullOrWhiteSpace(exportInfo.FulfilmentCenterId) ? Array.Empty <string>() : new[] { exportInfo.FulfilmentCenterId }, Take = int.MaxValue, }; var allProductInventories = (await _inventorySearchService.SearchInventoriesAsync(inventorySearchCriteria)).Results.ToList(); //Export configuration exportInfo.Configuration.PropertyCsvColumns = products.SelectMany(x => x.Properties).Select(x => x.Name).Distinct().ToArray(); csvWriter.Configuration.Delimiter = exportInfo.Configuration.Delimiter; csvWriter.Configuration.RegisterClassMap(new CsvProductMap(exportInfo.Configuration)); //Write header csvWriter.WriteHeader <CsvProduct>(); csvWriter.NextRecord(); prodgressInfo.TotalCount = products.Count; var notifyProductSizeLimit = 50; var counter = 0; //convert to dict for faster search var pricesDict = allProductPrices.GroupBy(x => x.ProductId).ToDictionary(x => x.Key, x => x.First()); var inventoriesDict = allProductInventories.GroupBy(x => x.ProductId).ToDictionary(x => x.Key, x => x.First()); foreach (var product in products) { try { var csvProducts = MakeMultipleExportProducts(product, pricesDict, inventoriesDict); csvWriter.WriteRecords(csvProducts); } catch (Exception ex) { prodgressInfo.Errors.Add(ex.ToString()); progressCallback(prodgressInfo); } //Raise notification each notifyProductSizeLimit products counter++; prodgressInfo.ProcessedCount = counter; prodgressInfo.Description = string.Format("{0} of {1} products processed", prodgressInfo.ProcessedCount, prodgressInfo.TotalCount); if (counter % notifyProductSizeLimit == 0 || counter == prodgressInfo.TotalCount) { progressCallback(prodgressInfo); } } } }