public void ShouldGetProductPricesInCuurencyForTheSpecifiedProducts() { // Arrange GetProductBulkPricesRequest request = new GetProductBulkPricesRequest(new[] { "ProductId1", "ProductId2" }, "PriceType"); request.CurrencyCode = "CAD"; GetProductBulkPricesResult result = new GetProductBulkPricesResult(); ServicePipelineArgs args = new ServicePipelineArgs(request, result); result.Prices.Add("ProductId1", null); this.client.GetProductPrices(Arg.Is <string[]>(argument => (argument.Length == 2) && (argument[0] == "ProductId1") && (argument[1] == "ProductId2")), "PriceType").Returns(new[] { new ProductPriceModel { ProductId = "ProductId1", Price = 2 }, new ProductPriceModel { ProductId = "ProductId2", Price = 1 } }); // Act this.processor.Process(args); // Assert result.Prices.Should().HaveCount(2); result.Prices["ProductId1"].Amount.Should().Be(2); result.Prices["ProductId1"].CurrencyCode.Should().Be("CAD"); result.Prices["ProductId2"].Amount.Should().Be(1); result.Prices["ProductId2"].CurrencyCode.Should().Be("CAD"); }
/// <summary> /// Processes the specified arguments. /// </summary> /// <param name="args">The arguments.</param> public override void Process(Commerce.Pipelines.ServicePipelineArgs args) { Assert.ArgumentNotNull(args, "args"); Assert.ArgumentNotNull(args.Request, "args.request"); Assert.ArgumentCondition(args.Request is GetProductBulkPricesRequest, "args.Request", "args.Request is GetProductBulkPricesRequest"); Assert.ArgumentCondition(args.Result is Sitecore.Commerce.Services.Prices.GetProductBulkPricesResult, "args.Result", "args.Result is GetProductBulkPricesResult"); GetProductBulkPricesRequest request = (GetProductBulkPricesRequest)args.Request; Sitecore.Commerce.Services.Prices.GetProductBulkPricesResult result = (Sitecore.Commerce.Services.Prices.GetProductBulkPricesResult)args.Result; Assert.ArgumentNotNull(request.ProductCatalogName, "request.ProductCatalogName"); Assert.ArgumentNotNull(request.ProductIds, "request.ProductIds"); Assert.ArgumentNotNull(request.PriceType, "request.PriceType"); var uniqueIds = request.ProductIds.ToList().Distinct(); var productSearchItemList = this.GetProductsFromIndex(request.ProductCatalogName, uniqueIds); foreach (var productSearchItem in productSearchItemList) { var foundId = uniqueIds.SingleOrDefault(x => x == productSearchItem.Name); if (foundId == null) { continue; } if (productSearchItem != null) { // SOLR returns 0 and not <null> for missing prices. We set the prices to null when 0 value is encountered. decimal?listPrice = !productSearchItem.OtherFields.ContainsKey("listprice") ? (decimal?)null : (productSearchItem.ListPrice == 0 ? (decimal?)null : (decimal)productSearchItem.ListPrice); decimal?adjustedPrice = !productSearchItem.OtherFields.ContainsKey("adjustedprice") ? (decimal?)null : (productSearchItem.AdjustedPrice == 0 ? (decimal?)null : (decimal)productSearchItem.AdjustedPrice); ExtendedCommercePrice extendedPrice = this.EntityFactory.Create <ExtendedCommercePrice>("Price"); if (adjustedPrice.HasValue) { extendedPrice.ListPrice = adjustedPrice.Value; } else { // No base price is defined, the List price is set to the actual ListPrice define in the catalog extendedPrice.ListPrice = (listPrice.HasValue) ? listPrice.Value : 0M; } // The product list price is the Connect "Adjusted" price. if (listPrice.HasValue) { extendedPrice.Amount = listPrice.Value; } result.Prices.Add(foundId, extendedPrice); } } }
/// <summary> /// Gets the product bulk prices. /// </summary> /// <param name="storefront">The storefront.</param> /// <param name="catalogName">Name of the catalog.</param> /// <param name="productIds">The product ids.</param> /// <param name="priceTypeIds">The price type ids.</param> /// <returns>The manager response with the list of prices in the Result.</returns> public virtual ManagerResponse <GetProductBulkPricesResult, IDictionary <string, Dictionary <string, Price> > > GetProductBulkPrices([NotNull] CommerceStorefront storefront, string catalogName, IEnumerable <string> productIds, params string[] priceTypeIds) { Assert.ArgumentNotNull(storefront, "storefront"); if (priceTypeIds == null) { priceTypeIds = defaultPriceTypeIds; } var request = new GetProductBulkPricesRequest(catalogName, productIds, priceTypeIds); var result = this.PricingServiceProvider.GetProductBulkPrices(request); // Currently, both Categories and Products are passed in and are waiting for a fix to filter the categories out. Until then, this code is commented // out as it generates an unecessary Error event indicating the product cannot be found. // Helpers.LogSystemMessages(result.SystemMessages, result); return(new ManagerResponse <GetProductBulkPricesResult, IDictionary <string, Dictionary <string, Price> > >(result, result.Prices == null ? new Dictionary <string, Dictionary <string, Price> >() : result.Prices)); }
public void ShouldNotAddNewRecordsIfProductPriceIsNotFound() { // Arrange GetProductBulkPricesRequest request = new GetProductBulkPricesRequest(new[] { "ProductId1" }, "PriceType"); GetProductBulkPricesResult result = new GetProductBulkPricesResult(); ServicePipelineArgs args = new ServicePipelineArgs(request, result); this.client.GetProductPrices(Arg.Is <string[]>(argument => (argument.Length == 1) && (argument[0] == "ProductId1")), "PriceType").Returns(new[] { new ProductPriceModel { ProductId = "ProductId1", Price = null } }); // Act this.processor.Process(args); // Assert result.Prices.Should().BeEmpty(); }
/// <summary> /// Gets the product bulk prices. /// </summary> /// <param name="productIds">The product ids.</param> /// <returns>The product bulk prices.</returns> public IDictionary <string, decimal> GetProductBulkPrices(IEnumerable <string> productIds) { var ids = productIds as IList <string> ?? productIds.ToList(); GetProductBulkPricesRequest request = new GetProductBulkPricesRequest(ids); GetProductBulkPricesResult result = this.serviceProvider.GetProductBulkPrices(request); var prices = new Dictionary <string, decimal>(ids.Count); foreach (var id in ids) { var price = result.Prices.ContainsKey(id) ? result.Prices[id].Amount : decimal.Zero; if (prices.ContainsKey(id)) { continue; } prices.Add(id, price); } return(prices); }
/// <summary> /// Processes the specified args. /// </summary> /// <param name="args">The args.</param> public override void Process(ServicePipelineArgs args) { GetProductBulkPricesRequest request = (GetProductBulkPricesRequest)args.Request; GetProductBulkPricesResult result = (GetProductBulkPricesResult)args.Result; ProductPriceModel[] productPriceModels; var currencyCode = request.CurrencyCode; if (string.IsNullOrEmpty(currencyCode)) { currencyCode = "USD"; } using (IPricesServiceChannel client = this.GetClient()) { productPriceModels = client.GetProductPrices(request.ProductIds.ToArray(), request.PriceType); } foreach (ProductPriceModel model in productPriceModels) { if (model.Price != null) { if (result.Prices.ContainsKey(model.ProductId)) { result.Prices[model.ProductId] = new Price(model.Price.Value, currencyCode) { PriceType = request.PriceType }; } else { result.Prices.Add(model.ProductId, new Price(model.Price.Value, currencyCode) { PriceType = request.PriceType }); } } } }
/// <summary> /// Processes the specified arguments. /// </summary> /// <param name="args">The arguments.</param> public override void Process(Commerce.Pipelines.ServicePipelineArgs args) { Assert.ArgumentNotNull(args, "args"); Assert.ArgumentNotNull(args.Request, "args.request"); Assert.ArgumentCondition(args.Request is GetProductBulkPricesRequest, "args.Request", "args.Request is GetProductBulkPricesRequest"); Assert.ArgumentCondition(args.Result is Sitecore.Commerce.Services.Prices.GetProductBulkPricesResult, "args.Result", "args.Result is GetProductBulkPricesResult"); GetProductBulkPricesRequest request = (GetProductBulkPricesRequest)args.Request; Sitecore.Commerce.Services.Prices.GetProductBulkPricesResult result = (Sitecore.Commerce.Services.Prices.GetProductBulkPricesResult)args.Result; Assert.ArgumentNotNull(request.ProductCatalogName, "request.ProductCatalogName"); Assert.ArgumentNotNull(request.ProductIds, "request.ProductIds"); Assert.ArgumentNotNull(request.PriceType, "request.PriceType"); bool isList = request.PriceTypeIds.FirstOrDefault(x => x.Equals(PriceTypes.List, StringComparison.OrdinalIgnoreCase)) != null; bool isAdjusted = request.PriceTypeIds.FirstOrDefault(x => x.Equals(PriceTypes.Adjusted, StringComparison.OrdinalIgnoreCase)) != null; bool isLowestPriceVariantSpecified = request.PriceTypeIds.FirstOrDefault(x => x.Equals(PriceTypes.LowestPricedVariant, StringComparison.OrdinalIgnoreCase)) != null; bool isLowestPriceVariantListPriceSpecified = request.PriceTypeIds.FirstOrDefault(x => x.Equals(PriceTypes.LowestPricedVariantListPrice, StringComparison.OrdinalIgnoreCase)) != null; bool isHighestPriceVariantSpecified = request.PriceTypeIds.FirstOrDefault(x => x.Equals(PriceTypes.HighestPricedVariant, StringComparison.OrdinalIgnoreCase)) != null; var uniqueIds = request.ProductIds.ToList().Distinct(); var productSearchItemList = this.GetProductsFromIndex(request.ProductCatalogName, uniqueIds); foreach (var productSearchItem in productSearchItemList) { var foundId = uniqueIds.SingleOrDefault(x => x == productSearchItem.Name); if (foundId == null) { continue; } if (productSearchItem != null) { // SOLR returns 0 and not <null> for missing prices. We set the prices to null when 0 value is encountered. decimal?listPrice = !productSearchItem.OtherFields.ContainsKey("listprice") ? (decimal?)null : ((decimal)productSearchItem.ListPrice == 0M ? (decimal?)null : (decimal)productSearchItem.ListPrice); decimal?basePrice = !productSearchItem.OtherFields.ContainsKey("baseprice") ? (decimal?)null : ((decimal)productSearchItem.BasePrice == 0M ? (decimal?)null : (decimal)productSearchItem.BasePrice); ExtendedCommercePrice extendedPrice = this.EntityFactory.Create <ExtendedCommercePrice>("Price"); // The product base price is the Connect "List" price. if (isList) { if (basePrice.HasValue) { extendedPrice.Amount = basePrice.Value; } else { // No base price is defined, the List price is set to the actual ListPrice define in the catalog extendedPrice.Amount = (listPrice.HasValue) ? listPrice.Value : 0M; } } // The product list price is the Connect "Adjusted" price. if (isAdjusted && listPrice.HasValue) { extendedPrice.ListPrice = listPrice.Value; } var variantInfoString = productSearchItem.VariantInfo; if ((isLowestPriceVariantSpecified || isLowestPriceVariantListPriceSpecified || isHighestPriceVariantSpecified) && !string.IsNullOrWhiteSpace(variantInfoString)) { List <VariantIndexInfo> variantIndexInfoList = JsonConvert.DeserializeObject <List <VariantIndexInfo> >(variantInfoString); this.SetVariantPricesFromProductVariants(productSearchItem, variantIndexInfoList, extendedPrice, isLowestPriceVariantSpecified, isLowestPriceVariantListPriceSpecified, isHighestPriceVariantSpecified); } result.Prices.Add(foundId, extendedPrice); } } }
/// <summary> /// Processes the specified arguments. /// </summary> /// <param name="args">The arguments.</param> public override void Process(Commerce.Pipelines.ServicePipelineArgs args) { Assert.ArgumentNotNull(args, "args"); Assert.ArgumentNotNull(args.Request, "args.request"); Assert.ArgumentCondition(args.Request is GetProductBulkPricesRequest, "args.Request", "args.Request is GetProductBulkPricesRequest"); Assert.ArgumentCondition(args.Result is GetProductBulkPricesResult, "args.Result", "args.Result is GetProductBulkPricesResult"); GetProductBulkPricesRequest request = (GetProductBulkPricesRequest)args.Request; GetProductBulkPricesResult result = (GetProductBulkPricesResult)args.Result; Assert.ArgumentNotNull(request.ProductCatalogName, "request.ProductCatalogName"); Assert.ArgumentNotNull(request.ProductIds, "request.ProductIds"); Assert.ArgumentNotNull(request.PriceType, "request.PriceType"); ICatalogRepository catalogRepository = CommerceTypeLoader.CreateInstance <ICatalogRepository>(); bool isList = Array.FindIndex(request.PriceTypeIds as string[], t => t.IndexOf("List", StringComparison.OrdinalIgnoreCase) >= 0) > -1; bool isAdjusted = Array.FindIndex(request.PriceTypeIds as string[], t => t.IndexOf("Adjusted", StringComparison.OrdinalIgnoreCase) >= 0) > -1; var uniqueIds = request.ProductIds.ToList().Distinct(); foreach (var requestedProductId in uniqueIds) { try { var product = catalogRepository.GetProductReadOnly(request.ProductCatalogName, requestedProductId); Dictionary <string, Price> prices = new Dictionary <string, Price>(); // BasePrice is a List price and ListPrice is Adjusted price if (isList) { if (product.HasProperty("BasePrice") && product["BasePrice"] != null) { prices.Add("List", new Price { PriceType = "List", Amount = (product["BasePrice"] as decimal?).Value }); } else { // No base price is defined, the List price is set to the actual ListPrice define in the catalog prices.Add("List", new Price { PriceType = "List", Amount = product.ListPrice }); } } if (isAdjusted && !product.IsListPriceNull()) { prices.Add("Adjusted", new Price { PriceType = "Adjusted", Amount = product.ListPrice }); } result.Prices.Add(requestedProductId, prices); } catch (CommerceServer.Core.EntityDoesNotExistException e) { result.Success = false; result.SystemMessages.Add(new SystemMessage { Message = e.Message }); continue; } } }
/// <summary> /// Gets the product bulk prices. /// </summary> /// <param name="request">The request.</param> /// <returns>Returns product bulk prices.</returns> public virtual GetProductBulkPricesResult GetProductBulkPrices(GetProductBulkPricesRequest request) { return(this.RunPipeline <GetProductBulkPricesRequest, GetProductBulkPricesResult>("commerce.prices.getProductBulkPrices", request)); }
/// <summary> /// Processes the specified arguments. /// </summary> /// <param name="args">The arguments.</param> public override void Process(Commerce.Pipelines.ServicePipelineArgs args) { Assert.ArgumentNotNull(args, "args"); Assert.ArgumentNotNull(args.Request, "args.request"); Assert.ArgumentCondition(args.Request is GetProductBulkPricesRequest, "args.Request", "args.Request is GetProductBulkPricesRequest"); Assert.ArgumentCondition(args.Result is Sitecore.Commerce.Services.Prices.GetProductBulkPricesResult, "args.Result", "args.Result is GetProductBulkPricesResult"); GetProductBulkPricesRequest request = (GetProductBulkPricesRequest)args.Request; Sitecore.Commerce.Services.Prices.GetProductBulkPricesResult result = (Sitecore.Commerce.Services.Prices.GetProductBulkPricesResult)args.Result; Assert.ArgumentNotNull(request.ProductCatalogName, "request.ProductCatalogName"); Assert.ArgumentNotNull(request.ProductIds, "request.ProductIds"); Assert.ArgumentNotNull(request.PriceType, "request.PriceType"); ICatalogRepository catalogRepository = ContextTypeLoader.CreateInstance <ICatalogRepository>(); bool isList = request.PriceTypeIds.FirstOrDefault(x => x.Equals(PriceTypes.List, StringComparison.OrdinalIgnoreCase)) != null; bool isAdjusted = request.PriceTypeIds.FirstOrDefault(x => x.Equals(PriceTypes.Adjusted, StringComparison.OrdinalIgnoreCase)) != null; bool isLowestPriceVariantSpecified = request.PriceTypeIds.FirstOrDefault(x => x.Equals(PriceTypes.LowestPricedVariant, StringComparison.OrdinalIgnoreCase)) != null; bool isLowestPriceVariantListPriceSpecified = request.PriceTypeIds.FirstOrDefault(x => x.Equals(PriceTypes.LowestPricedVariantListPrice, StringComparison.OrdinalIgnoreCase)) != null; bool isHighestPriceVariantSpecified = request.PriceTypeIds.FirstOrDefault(x => x.Equals(PriceTypes.HighestPricedVariant, StringComparison.OrdinalIgnoreCase)) != null; var uniqueIds = request.ProductIds.ToList().Distinct(); foreach (var requestedProductId in uniqueIds) { try { var product = catalogRepository.GetProductReadOnly(request.ProductCatalogName, requestedProductId); ExtendedCommercePrice extendedPrice = this.EntityFactory.Create <ExtendedCommercePrice>("Price"); // BasePrice is a List price and ListPrice is Adjusted price if (isList) { if (product.HasProperty("BasePrice") && product["BasePrice"] != null) { extendedPrice.Amount = (product["BasePrice"] as decimal?).Value; } else { // No base price is defined, the List price is set to the actual ListPrice define in the catalog extendedPrice.Amount = product.ListPrice; } } if (isAdjusted && !product.IsListPriceNull()) { extendedPrice.ListPrice = product.ListPrice; } if ((isLowestPriceVariantSpecified || isLowestPriceVariantListPriceSpecified || isHighestPriceVariantSpecified) && product is ProductFamily) { this.SetVariantPrices(product as ProductFamily, extendedPrice, isLowestPriceVariantSpecified, isLowestPriceVariantListPriceSpecified, isHighestPriceVariantSpecified); } result.Prices.Add(requestedProductId, extendedPrice); } catch (EntityDoesNotExistException e) { result.Success = false; result.SystemMessages.Add(new SystemMessage { Message = e.Message }); continue; } } }