/// <summary> /// Initializes the specified rendering. /// </summary> /// <param name="rendering">The rendering.</param> /// <param name="searchResult">The search result.</param> public virtual void Initialize(Rendering rendering, SearchResults searchResult) { base.Initialize(rendering); if (searchResult == null) { return; } this.DisplayName = searchResult.DisplayName; this.Products = new List<ProductViewModel>(); foreach (var child in searchResult.SearchResultItems) { var productModel = new ProductViewModel(child); productModel.Initialize(this.Rendering); this.Products.Add(productModel); } }
protected IEnumerable<CategoryViewModel> GroupRelationshipsByDescription([NotNull] CommerceStorefront storefront, RelationshipField field, IEnumerable<string> relationshipNames, IEnumerable<CatalogRelationshipInformation> productRelationshipInfoList, Rendering rendering) { Dictionary<string, CategoryViewModel> relationshipGroups = new Dictionary<string, CategoryViewModel>(StringComparer.OrdinalIgnoreCase); if (field != null && productRelationshipInfoList != null) { foreach (var relationshipInfo in productRelationshipInfoList) { if (!relationshipNames.Any() || relationshipNames.Contains(relationshipInfo.RelationshipName, StringComparer.OrdinalIgnoreCase)) { var relationshipDescription = string.IsNullOrWhiteSpace(relationshipInfo.RelationshipDescription) ? relationshipInfo.RelationshipName : relationshipInfo.RelationshipDescription; CategoryViewModel categoryModel = null; if (!relationshipGroups.TryGetValue(relationshipDescription, out categoryModel)) { categoryModel = new CategoryViewModel { ChildProducts = new List<ProductViewModel>(), RelationshipName = relationshipInfo.RelationshipName, RelationshipDescription = relationshipDescription }; relationshipGroups[relationshipDescription] = categoryModel; } var targetItemId = ID.Parse(relationshipInfo.ToItemExternalId); var targetItem = field.InnerField.Database.GetItem(targetItemId); var productModel = new ProductViewModel(targetItem); productModel.Initialize(rendering); this.GetProductRating(targetItem); categoryModel.ChildProducts.Add(productModel); } } } if (relationshipGroups.Count > 0) { List<ProductViewModel> productViewModelList = new List<ProductViewModel>(); foreach (string key in relationshipGroups.Keys) { CategoryViewModel viewModel = relationshipGroups[key]; var childProducts = viewModel.ChildProducts; if (childProducts != null && childProducts.Count > 0) { productViewModelList.AddRange(childProducts); } } if (productViewModelList.Count > 0) { this.GetProductBulkPrices(productViewModelList); this.InventoryManager.GetProductsStockStatus(storefront, productViewModelList); } } return relationshipGroups.Values; }
/// <summary> /// Gets the product price. /// </summary> /// <param name="productViewModel">The product view model.</param> public virtual void GetProductPrice(ProductViewModel productViewModel) { if (productViewModel == null) { return; } bool includeVariants = productViewModel.Variants != null && productViewModel.Variants.Count > 0; var pricesResponse = this.PricingManager.GetProductPrices(StorefrontManager.CurrentStorefront, productViewModel.CatalogName, productViewModel.ProductId, includeVariants, null); if (pricesResponse != null && pricesResponse.ServiceProviderResult.Success && pricesResponse.Result != null) { Price price; Dictionary<string, Price> prices; if (pricesResponse.Result.TryGetValue(productViewModel.ProductId, out prices)) { if (prices.TryGetValue("List", out price)) { productViewModel.ListPrice = price.Amount; } if (prices.TryGetValue("Adjusted", out price)) { productViewModel.AdjustedPrice = price.Amount; } } if (includeVariants) { foreach (var variant in productViewModel.Variants) { if (pricesResponse.Result.TryGetValue(variant.VariantId, out prices)) { if (prices.TryGetValue("List", out price)) { variant.ListPrice = price.Amount; } if (prices.TryGetValue("Adjusted", out price)) { variant.AdjustedPrice = price.Amount; } } } } } }
/// <summary> /// Gets the product price. /// </summary> /// <param name="productViewModel">The product view model.</param> public virtual void GetProductPrice(ProductViewModel productViewModel) { if (productViewModel == null) { return; } bool includeVariants = productViewModel.Variants != null && productViewModel.Variants.Count > 0; var pricesResponse = this.PricingManager.GetProductPrices(StorefrontManager.CurrentStorefront, productViewModel.CatalogName, productViewModel.ProductId, includeVariants, null); if (pricesResponse != null && pricesResponse.ServiceProviderResult.Success && pricesResponse.Result != null) { Price price; if (pricesResponse.Result.TryGetValue(productViewModel.ProductId, out price)) { ExtendedCommercePrice extendedPrice = (ExtendedCommercePrice)price; productViewModel.ListPrice = price.Amount; productViewModel.AdjustedPrice = extendedPrice.ListPrice; } if (includeVariants) { foreach (var variant in productViewModel.Variants) { if (pricesResponse.Result.TryGetValue(variant.VariantId, out price)) { ExtendedCommercePrice extendedPrice = (ExtendedCommercePrice)price; variant.ListPrice = extendedPrice.Amount; variant.AdjustedPrice = extendedPrice.ListPrice; } } } } }
/// <summary> /// Gets the product price. /// </summary> /// <param name="productViewModel">The product view model.</param> public virtual void GetProductPrice(ProductViewModel productViewModel) { if (productViewModel == null) { return; } var pricesResponse = this.PricingManager.GetProductPrices(StorefrontManager.CurrentStorefront, productViewModel.ProductId, null); if (pricesResponse != null && pricesResponse.ServiceProviderResult.Success && pricesResponse.Result != null) { Price price; if (pricesResponse.Result.TryGetValue(productViewModel.ProductId, out price)) { productViewModel.ListPrice = price.Amount; productViewModel.AdjustedPrice = ((CommercePrice)price).ListPrice; // The current implementation assumes all variants have the same price as the product. We need to set the variant // prices so they will render properly. if (productViewModel.Variants.Any()) { foreach (var variant in productViewModel.Variants) { variant.ListPrice = productViewModel.ListPrice; variant.AdjustedPrice = productViewModel.AdjustedPrice; } } } } }