/// <summary> /// Return the initial state of the KVA selector /// The default implementation initialize the KVA with the selected Variant; /// A common override is to initialize the KVA only if the selected Variant match the requested Variant /// /// </summary> /// <param name="selectedVariant"></param> /// <param name="param"></param> /// <returns></returns> protected virtual Dictionary <string, object> GetSelectedKvas( VariantViewModel selectedVariant, CreateProductDetailViewModelParam param) { return(selectedVariant == null ? new Dictionary <string, object>() : selectedVariant.Kvas); }
protected virtual ProductViewModel CreateViewModel(CreateProductDetailViewModelParam param) { if (param == null) { throw new ArgumentNullException(nameof(param)); } if (param.Product == null) { throw new ArgumentException(GetMessageOfNull(nameof(param.Product)), nameof(param)); } if (param.ProductDefinition == null) { throw new ArgumentException(GetMessageOfNull(nameof(param.ProductDefinition)), nameof(param)); } if (param.ProductLookups == null) { throw new ArgumentException(GetMessageOfNull(nameof(param.ProductLookups)), nameof(param)); } if (param.CultureInfo == null) { throw new ArgumentException(GetMessageOfNull(nameof(param.CultureInfo)), nameof(param)); } if (string.IsNullOrEmpty(param.BaseUrl)) { throw new ArgumentException(GetMessageOfNullEmpty(nameof(param.BaseUrl)), nameof(param)); } var productDetailViewModel = ViewModelMapper.MapTo <ProductViewModel>(param.Product, param.CultureInfo); InitializeProductImages(param.Product.Id, param.ProductDetailImages, param.CultureInfo, productDetailViewModel); var productDisplayName = productDetailViewModel.DisplayName ?? string.Empty; var allVariantsVm = GetVariantViewModels( param.Product.Variants, param.ProductDefinition.VariantProperties, productDisplayName, param.CultureInfo, vvm => InitializeVariantImages(param.Product.Id, param.ProductDetailImages, param.CultureInfo, vvm), vvm => InitializeVariantSpecificaton(param.Product, param.ProductDefinition, vvm) ).ToList(); productDetailViewModel.Variants = allVariantsVm; var selectedVariantVm = GetSelectedVariantViewModel(param.VariantId, allVariantsVm); MergeSelectedVariantVmToProductVm(selectedVariantVm, productDetailViewModel); var selectedKvas = GetSelectedKvas(selectedVariantVm, param); productDetailViewModel.Context["selectedKvas"] = selectedKvas; //todo: create factory? the code to build variants is huge // get all the key variant attributes possibilities. For instance, if the product has colors and size. This method // will produce two collections (colors, sizes) and for each collection, it will contain all the possible values. productDetailViewModel.KeyVariantAttributeItems = CreateKvaItems(new GenerateKvaItemsParam { ProductLookups = param.ProductLookups, Product = param.Product, SelectedKvas = selectedKvas, ProductDefinition = param.ProductDefinition, CultureInfo = param.CultureInfo, ProductVariants = allVariantsVm }); productDetailViewModel.Price = param.Product.ListPrice; productDetailViewModel.ProductDetailUrl = ProductUrlProvider.GetProductUrl(new GetProductUrlParam { CultureInfo = param.CultureInfo, ProductId = param.Product.Id, VariantId = param.VariantId, SKU = param.Product.Sku, ProductName = productDisplayName }); productDetailViewModel.CreateAccountUrl = MyAccountUrlProvider.GetCreateAccountUrl(new BaseUrlParameter { CultureInfo = param.CultureInfo, ReturnUrl = productDetailViewModel.ProductDetailUrl }); SetViewModelContext(productDetailViewModel, selectedVariantVm, allVariantsVm); // When the product lookup is loaded, only the keys of the lookup are assigned to the product view model. This method // compare the product view model bag item keys with the lookups name and assign the right localized lookup values to the view model. AssignLookupValuesToProductPropertyBag(param.ProductDefinition, param.ProductLookups, param.CultureInfo, productDetailViewModel); productDetailViewModel.Description = FixHtml(productDetailViewModel.Description); productDetailViewModel.DefinitionName = param.ProductDefinition.Name; if (ProductConfiguration.IsQuantityDisplayed && ProductConfiguration.MinQuantity > 0 && ProductConfiguration.MaxQuantity >= ProductConfiguration.MinQuantity) { productDetailViewModel.Quantity = new ProductQuantityViewModel { Min = ProductConfiguration.MinQuantity, Max = ProductConfiguration.MaxQuantity, Value = ProductConfiguration.MinQuantity }; } productDetailViewModel.Currency = param.Currency; productDetailViewModel.Specifications = ProductSpecificationsViewService.GetProductSpecificationsViewModel(new GetProductSpecificationsParam { Product = param.Product, ProductDefinition = param.ProductDefinition }); return(productDetailViewModel); }