private SearchProductAttributeModel PrepareProductAttributeModel(CategoryProductAttribute attribute) { var attributeModel = new SearchProductAttributeModel(); attributeModel.AttributeTitle = attribute.ProductAttribute.Name; attributeModel.Id = attribute.Id; attributeModel.PictureThumbnailUrl = _pictureService.GetPictureUrl(attribute.ProductAttribute.PictureId.GetValueOrDefault(), 100, false); attributeModel.Description = attribute.ProductAttribute.Description; attributeModel.AttributeControlTypeId = attribute.SearchControlTypeId; switch (attribute.SearchControlType) { case SearchAttributeControlType.CheckBox: case SearchAttributeControlType.CheckBoxGroup: { var attributeValues = attribute.CategoryProductAttributeValues; if (attributeValues.Count > 0) { attributeModel.Values = new List <SearchProductAttributeValueModel>(); foreach (var value in attributeValues.OrderBy(x => x.DisplayOrder)) { var attributeValueModel = new SearchProductAttributeValueModel(); attributeValueModel.Id = value.Id; attributeValueModel.Selected = value.IsPreSelected; attributeValueModel.ValueText = value.Name; attributeValueModel.Popularvalue = value.PopularValue; attributeModel.Values.Add(attributeValueModel); } } break; } case SearchAttributeControlType.DropDown: { var attributeValues = attribute.CategoryProductAttributeValues; if (attributeValues.Count > 0) { attributeModel.Values = new List <SearchProductAttributeValueModel>(); //if (attribute.CategoryProductAttributeValues.FirstOrDefault(x => x.IsPreSelected) == null) //{ // attributeModel.Values.Add(new SearchProductAttributeValueModel() // { // Id = 0, // ValueText = attribute.ProductAttribute.Name, // Selected = true // }); //} if (attributeModel.Values.FirstOrDefault(x => x.Selected) == null) { attributeModel.Values.Add(new SearchProductAttributeValueModel() { Id = 0, ValueText = attribute.MainAttribute ? attribute.ProductAttribute.Name : _localizationService.GetResource("ITBFA.Attribute.NoImportant"), Selected = true }); } else { if (attributeModel.Values.FirstOrDefault(x => x.Id == 0) == null) { attributeModel.Values.Add(new SearchProductAttributeValueModel() { Id = 0, ValueText = attribute.MainAttribute ? attribute.ProductAttribute.Name : _localizationService.GetResource("ITBFA.Attribute.NoImportant"), Selected = false }); } } foreach (var value in attributeValues.OrderBy(x => x.DisplayOrder)) { var attributeValueModel = new SearchProductAttributeValueModel(); attributeValueModel.Id = value.Id; attributeValueModel.ValueText = value.Name; attributeValueModel.Popularvalue = value.PopularValue; attributeModel.Values.Add(attributeValueModel); if (value.IsPreSelected) { attributeModel.SelectedAttributeId = value.Id; } } } break; } case SearchAttributeControlType.ToddlerIntBetween: case SearchAttributeControlType.ToddlerMax: case SearchAttributeControlType.ToddlerMin: { var values = attribute.CategoryProductAttributeValues.Where(x => x.RealValue.HasValue) .Where(x => x.Products.Where(p => !p.Deleted).Any() || x.Products.Count == 0); if (!values.Any()) { attributeModel.MinValue = 0; attributeModel.MinValue = int.MaxValue; } else { values = values.OrderByDescending(x => x.RealValue).ToList(); attributeModel.MinValue = (int)values.Last().RealValue; attributeModel.MaxValue = (int)values.First().RealValue; attributeModel.Values = new List <SearchProductAttributeValueModel>(); foreach (var val in attribute.CategoryProductAttributeValues.Where(x => x.PopularValue).Where(x => x.RealValue.HasValue)) { var popularValue = new SearchProductAttributeValueModel(); popularValue.ValueDouble = ((int)val.RealValue).ToString(); attributeModel.Values.Add(popularValue); } } break; } } return(attributeModel); }
private IList <SearchProductAttributeValue> PrepareAttributeModelForSearch(SearchProductAttributeModel model) { var attributesForSearch = new List <SearchProductAttributeValue>(); switch ((SearchAttributeControlType)model.AttributeControlTypeId) { case SearchAttributeControlType.CheckBox: case SearchAttributeControlType.CheckBoxGroup: { foreach (var value in model.Values) { if (value.Selected) { var attributeForSearch = new SearchProductAttributeValue(); attributeForSearch.CategoryProductAttributeId = model.Id; attributeForSearch.IdValue = value.Id; attributeForSearch.ControlType = (SearchAttributeControlType)model.AttributeControlTypeId; attributesForSearch.Add(attributeForSearch); } } break; } case SearchAttributeControlType.DropDown: { if (model.SelectedAttributeId != 0) { var attributeForSearch = new SearchProductAttributeValue(); attributeForSearch.CategoryProductAttributeId = model.Id; attributeForSearch.IdValue = model.SelectedAttributeId; attributeForSearch.ControlType = (SearchAttributeControlType)model.AttributeControlTypeId; attributesForSearch.Add(attributeForSearch); } break; } case SearchAttributeControlType.TextBoxText: { if (!String.IsNullOrEmpty(model.AttributeValue)) { var attributeForSearch = new SearchProductAttributeValue(); attributeForSearch.CategoryProductAttributeId = model.Id; attributeForSearch.Textvalue = model.AttributeValue; attributeForSearch.ControlType = (SearchAttributeControlType)model.AttributeControlTypeId; attributesForSearch.Add(attributeForSearch); } break; } case SearchAttributeControlType.TextBoxReal: { if (!String.IsNullOrEmpty(model.AttributeValue)) { var attributeForSearch = new SearchProductAttributeValue(); attributeForSearch.CategoryProductAttributeId = model.Id; double res = 0; double.TryParse(model.AttributeValue, out res); attributeForSearch.ExactValue = res; attributeForSearch.ControlType = (SearchAttributeControlType)model.AttributeControlTypeId; attributesForSearch.Add(attributeForSearch); } break; } case SearchAttributeControlType.ToddlerMax: { if (model.MinValue != 0 && model.SelectedIntValue != 0) { var attributeForSearch = new SearchProductAttributeValue(); attributeForSearch.CategoryProductAttributeId = model.Id; attributeForSearch.ControlType = (SearchAttributeControlType)model.AttributeControlTypeId; attributeForSearch.MinValue = model.MinValue; attributeForSearch.MaxValue = model.SelectedIntValue; attributesForSearch.Add(attributeForSearch); } break; } case SearchAttributeControlType.ToddlerMin: { if (model.MaxValue != 0 && model.SelectedIntValue != 0) { var attributeForSearch = new SearchProductAttributeValue(); attributeForSearch.CategoryProductAttributeId = model.Id; attributeForSearch.ControlType = (SearchAttributeControlType)model.AttributeControlTypeId; attributeForSearch.MinValue = model.SelectedIntValue; attributeForSearch.MaxValue = model.MaxValue; attributesForSearch.Add(attributeForSearch); } break; } case SearchAttributeControlType.ToddlerIntBetween: { if (model.SelectedMaxIntValue != 0 && model.SelectedIntValue != 0) { var attributeForSearch = new SearchProductAttributeValue(); attributeForSearch.CategoryProductAttributeId = model.Id; attributeForSearch.ControlType = (SearchAttributeControlType)model.AttributeControlTypeId; attributeForSearch.MinValue = model.SelectedIntValue; attributeForSearch.MaxValue = model.SelectedMaxIntValue; attributesForSearch.Add(attributeForSearch); } break; } case SearchAttributeControlType.Money: { if (!String.IsNullOrEmpty(model.AttributeValue) || !String.IsNullOrEmpty(model.AttributeValueMax)) { var currency = _currencyService.GetCurrencyById(model.CurrencyId); var attributeForSearch = new SearchProductAttributeValue(); attributeForSearch.CategoryProductAttributeId = model.Id; attributeForSearch.ControlType = (SearchAttributeControlType)model.AttributeControlTypeId; if (!String.IsNullOrEmpty(model.AttributeValue)) { double res = 0; double.TryParse(model.AttributeValue, out res); attributeForSearch.MinValue = res / ((double)currency.Rate); attributeForSearch.MaxValue = res / ((double)currency.Rate); } attributesForSearch.Add(attributeForSearch); } break; } } return(attributesForSearch); }
private SearchProductCategoryModel PrepareCustomerAttributes() { var model = new SearchProductCategoryModel(); model.Currencies = _currencyService.GetAllCurrencies().Select(x => new CurrencyModel() { Id = x.Id, Name = x.CurrencyCode }).ToList(); model.Attributes = new List <SearchProductAttributeModel>(); model.Cities = _cityService.GetAllCities().OrderBy(x => x.Title).Select(x => new CityModel() { Id = x.Id, Title = x.Title }).ToList(); var customerAttributes = _customerInformationAttributeService.GetAllAttributes().OrderByDescending(x => x.DisplayOrder); foreach (var attribute in customerAttributes) { var attributeModel = new SearchProductAttributeModel(); attributeModel.Id = attribute.Id; attributeModel.AttributeControlTypeId = attribute.ProductSearchControlTypeId; attributeModel.AttributeTitle = typeof(Customer).GetProperty(attribute.CustomerFieldName) .GetCustomAttribute <CustomerFieldIsInProductAttribute>().Alias; switch (attribute.ProductSearchControlType) { case CustomerInformationProductSearchControlType.Gender: { attributeModel.Values = new List <SearchProductAttributeValueModel>(); var values = _customerInformationAttributeService.GetAttributeValuesForSearchProduct(attribute.Id); foreach (var val in values) { var attributeValue = new SearchProductAttributeValueModel(); var referenceValue = _customerInformationAttributeService.GetValue(val.Id); attributeValue.Id = val.Id; attributeValue.ValueText = referenceValue.Text; if (_workContext.CurrentCustomer.Gender.HasValue) { attributeValue.Selected = _workContext.CurrentCustomer.Gender == referenceValue.Id; } attributeModel.Values.Add(attributeValue); } break; } case CustomerInformationProductSearchControlType.NumberTextBoxExact: case CustomerInformationProductSearchControlType.NumberToddlerBetween: case CustomerInformationProductSearchControlType.NumberToddlerLess: case CustomerInformationProductSearchControlType.NumberToddlerMore: case CustomerInformationProductSearchControlType.MoneyLess: case CustomerInformationProductSearchControlType.MoneyMore: { if (_workContext.CurrentCustomer.IsRegistered()) { var val = _customerInformationAttributeService.GetCustomerAttribute(attribute.Id, _workContext.CurrentCustomer); attributeModel.AttributeValue = val.Text; attributeModel.CurrencyId = val.Id; } break; } } model.Attributes.Add(attributeModel); } return(model); }