/// <summary> /// Prepare model /// </summary> /// <param name="alreadyFilteredSpecOptionIds">IDs of already filtered specification options</param> /// <param name="filterableSpecificationAttributeOptionIds">IDs of filterable specification options</param> /// <param name="specificationAttributeService"></param> /// <param name="localizationService">Localization service</param> /// <param name="webHelper">Web helper</param> /// <param name="workContext">Work context</param> /// <param name="cacheManager">Cache manager</param> public virtual void PrepareSpecsFilters(IList <int> alreadyFilteredSpecOptionIds, int[] filterableSpecificationAttributeOptionIds, ISpecificationAttributeService specificationAttributeService, ILocalizationService localizationService, IWebHelper webHelper, IWorkContext workContext, ICacheManager cacheManager, IEnumerable <int> allProductsIds, IEnumerable <int> allFilteredProductsIds) { Enabled = false; var optionIds = filterableSpecificationAttributeOptionIds != null ? string.Join(",", filterableSpecificationAttributeOptionIds) : string.Empty; var cacheKey = string.Format(ModelCacheEventConsumer.SPECS_FILTER_MODEL_KEY, optionIds, workContext.WorkingLanguage.Id); var allOptions = specificationAttributeService.GetSpecificationAttributeOptionsByIds(filterableSpecificationAttributeOptionIds); var allFilters = cacheManager.Get(cacheKey, () => allOptions.Select(sao => new SpecificationAttributeOptionFilter { SpecificationAttributeId = sao.SpecificationAttribute.Id, SpecificationAttributeName = localizationService.GetLocalized(sao.SpecificationAttribute, x => x.Name, workContext.WorkingLanguage.Id), SpecificationAttributeDisplayOrder = sao.SpecificationAttribute.DisplayOrder, SpecificationAttributeOptionId = sao.Id, SpecificationAttributeOptionName = localizationService.GetLocalized(sao, x => x.Name, workContext.WorkingLanguage.Id), //SpecificationAttributeOptionColorRgb = sao.ColorSquaresRgb, SpecificationAttributeOptionDisplayOrder = sao.DisplayOrder }).ToList(), 24 * 60); if (!allFilters.Any()) { return; } //sort loaded options allFilters = allFilters.OrderBy(saof => saof.SpecificationAttributeDisplayOrder) .ThenBy(saof => saof.SpecificationAttributeName) .ThenBy(saof => saof.SpecificationAttributeOptionDisplayOrder) .ThenBy(saof => saof.SpecificationAttributeOptionName).ToList(); //prepare the model properties Enabled = true; var removeFilterUrl = webHelper.RemoveQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM); RemoveFilterUrl = ExcludeQueryStringParams(removeFilterUrl, webHelper); FilterItems = allFilters.Select(x => new SpecificationFilterItem { SpecificationAttributeName = x.SpecificationAttributeName, SpecificationAttributeOptionId = x.SpecificationAttributeOptionId, SpecificationAttributeOptionName = x.SpecificationAttributeOptionName, ProductsWithSpecificationCount = specificationAttributeService.GetProductsCountBySpecification(x.SpecificationAttributeOptionId, allProductsIds), FilteredProductsWithSpecificationCount = specificationAttributeService.GetProductsCountBySpecification(x.SpecificationAttributeOptionId, allFilteredProductsIds) }).ToList(); //get already filtered specification options var alreadyFilteredOptions = allFilters.Where(x => alreadyFilteredSpecOptionIds.Contains(x.SpecificationAttributeOptionId)); AlreadyFilteredItems = alreadyFilteredOptions.Select(x => new SpecificationFilterItem { SpecificationAttributeName = x.SpecificationAttributeName, SpecificationAttributeOptionName = x.SpecificationAttributeOptionName, SpecificationAttributeOptionId = x.SpecificationAttributeOptionId //SpecificationAttributeOptionColorRgb = x.SpecificationAttributeOptionColorRgb }).ToList(); //get not filtered specification options NotFilteredItems = allFilters.Except(alreadyFilteredOptions).Select(x => { //filter URL //var alreadyFiltered = alreadyFilteredSpecOptionIds.Concat(new List<int> { x.SpecificationAttributeOptionId }); //var filterUrl = webHelper.ModifyQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM, // alreadyFiltered.OrderBy(id => id).Select(id => id.ToString()).ToArray()); return(new SpecificationFilterItem() { SpecificationAttributeName = x.SpecificationAttributeName, SpecificationAttributeOptionName = x.SpecificationAttributeOptionName, //SpecificationAttributeOptionColorRgb = x.SpecificationAttributeOptionColorRgb, //FilterUrl = ExcludeQueryStringParams(filterUrl, webHelper), SpecificationAttributeOptionId = x.SpecificationAttributeOptionId }); }).ToList(); }