public ActionResult Search(SearchModel model, CatalogPagingFilteringModel command) { //'Continue shopping' URL _genericAttributeService.SaveAttribute(_workContext.CurrentCustomer, SystemCustomerAttributeNames.LastContinueShoppingPage, _webHelper.GetThisPageUrl(false), _storeContext.CurrentStore.Id); if (model == null) model = new SearchModel(); if (model.Q == null) model.Q = ""; model.Q = model.Q.Trim(); //sorting PrepareSortingOptions(model.PagingFilteringContext, command); //view mode PrepareViewModes(model.PagingFilteringContext, command); //page size PreparePageSizeOptions(model.PagingFilteringContext, command, _catalogSettings.SearchPageAllowCustomersToSelectPageSize, _catalogSettings.SearchPagePageSizeOptions, _catalogSettings.SearchPageProductsPerPage); var customerRolesIds = _workContext.CurrentCustomer.CustomerRoles .Where(cr => cr.Active).Select(cr => cr.Id).ToList(); string cacheKey = string.Format(ModelCacheEventConsumer.SEARCH_CATEGORIES_MODEL_KEY, _workContext.WorkingLanguage.Id, string.Join(",", customerRolesIds), _storeContext.CurrentStore.Id); var categories = _cacheManager.Get(cacheKey, () => { var categoriesModel = new List<SearchModel.CategoryModel>(); //all categories foreach (var c in _categoryService.GetAllCategories()) { //generate full category name (breadcrumb) string categoryBreadcrumb= ""; var breadcrumb = c.GetCategoryBreadCrumb(_categoryService, _aclService, _storeMappingService); for (int i = 0; i <= breadcrumb.Count - 1; i++) { categoryBreadcrumb += breadcrumb[i].GetLocalized(x => x.Name); if (i != breadcrumb.Count - 1) categoryBreadcrumb += " >> "; } categoriesModel.Add(new SearchModel.CategoryModel() { Id = c.Id, Breadcrumb = categoryBreadcrumb }); } return categoriesModel; }); if (categories.Count > 0) { //first empty entry model.AvailableCategories.Add(new SelectListItem() { Value = "0", Text = _localizationService.GetResource("Common.All") }); //all other categories foreach (var c in categories) { model.AvailableCategories.Add(new SelectListItem() { Value = c.Id.ToString(), Text = c.Breadcrumb, Selected = model.Cid == c.Id }); } } var manufacturers = _manufacturerService.GetAllManufacturers(); if (manufacturers.Count > 0) { model.AvailableManufacturers.Add(new SelectListItem() { Value = "0", Text = _localizationService.GetResource("Common.All") }); foreach (var m in manufacturers) model.AvailableManufacturers.Add(new SelectListItem() { Value = m.Id.ToString(), Text = m.GetLocalized(x => x.Name), Selected = model.Mid == m.Id }); } IPagedList<Product> products = new PagedList<Product>(new List<Product>(), 0, 1); // only search if query string search keyword is set (used to avoid searching or displaying search term min length error message on /search page load) if (Request.Params["Q"] != null) { if (model.Q.Length < _catalogSettings.ProductSearchTermMinimumLength) { model.Warning = string.Format(_localizationService.GetResource("Search.SearchTermMinimumLengthIsNCharacters"), _catalogSettings.ProductSearchTermMinimumLength); } else { var categoryIds = new List<int>(); int manufacturerId = 0; decimal? minPriceConverted = null; decimal? maxPriceConverted = null; bool searchInDescriptions = false; if (model.As) { //advanced search var categoryId = model.Cid; if (categoryId > 0) { categoryIds.Add(categoryId); if (model.Isc) { //include subcategories categoryIds.AddRange(GetChildCategoryIds(categoryId)); } } manufacturerId = model.Mid; //min price if (!string.IsNullOrEmpty(model.Pf)) { decimal minPrice = decimal.Zero; if (decimal.TryParse(model.Pf, out minPrice)) minPriceConverted = _currencyService.ConvertToPrimaryStoreCurrency(minPrice, _workContext.WorkingCurrency); } //max price if (!string.IsNullOrEmpty(model.Pt)) { decimal maxPrice = decimal.Zero; if (decimal.TryParse(model.Pt, out maxPrice)) maxPriceConverted = _currencyService.ConvertToPrimaryStoreCurrency(maxPrice, _workContext.WorkingCurrency); } searchInDescriptions = model.Sid; } //var searchInProductTags = false; var searchInProductTags = searchInDescriptions; //products products = _productService.SearchProducts( categoryIds: categoryIds, manufacturerId: manufacturerId, storeId: _storeContext.CurrentStore.Id, visibleIndividuallyOnly: true, priceMin: minPriceConverted, priceMax: maxPriceConverted, keywords:model.Q, searchDescriptions: searchInDescriptions, searchSku: searchInDescriptions, searchProductTags: searchInProductTags, languageId: _workContext.WorkingLanguage.Id, orderBy: (ProductSortingEnum)command.OrderBy, pageIndex: command.PageNumber - 1, pageSize: command.PageSize); model.Products = PrepareProductOverviewModels(products).ToList(); model.NoResults = !model.Products.Any(); //search term statistics if (!String.IsNullOrEmpty(model.Q)) { var searchTerm = _searchTermService.GetSearchTermByKeyword(model.Q, _storeContext.CurrentStore.Id); if (searchTerm != null) { searchTerm.Count++; _searchTermService.UpdateSearchTerm(searchTerm); } else { searchTerm = new SearchTerm() { Keyword = model.Q, StoreId = _storeContext.CurrentStore.Id, Count = 1 }; _searchTermService.InsertSearchTerm(searchTerm); } } //event _eventPublisher.Publish(new ProductSearchEvent() { SearchTerm = model.Q, SearchInDescriptions = searchInDescriptions, CategoryIds = categoryIds, ManufacturerId = manufacturerId, WorkingLanguageId = _workContext.WorkingLanguage.Id }); } } model.PagingFilteringContext.LoadPagedList(products); return View(model); }
public ActionResult Search(SearchModel model, SearchPagingFilteringModel command) { if (model == null) model = new SearchModel(); //'Continue shopping' URL _customerService.SaveCustomerAttribute(_workContext.CurrentCustomer, SystemCustomerAttributeNames.LastContinueShoppingPage, _webHelper.GetThisPageUrl(false)); if (command.PageSize <= 0) command.PageSize = _catalogSettings.SearchPageProductsPerPage; if (command.PageNumber <= 0) command.PageNumber = 1; if (model.Q == null) model.Q = ""; model.Q = model.Q.Trim(); var categories = _categoryService.GetAllCategories(); if (categories.Count > 0) { model.AvailableCategories.Add(new SelectListItem() { Value = "0", Text = _localizationService.GetResource("Common.All") }); foreach(var c in categories) model.AvailableCategories.Add(new SelectListItem() { Value = c.Id.ToString(), Text = c.GetCategoryBreadCrumb(_categoryService), Selected = model.Cid == c.Id }); } var manufacturers = _manufacturerService.GetAllManufacturers(); if (manufacturers.Count > 0) { model.AvailableManufacturers.Add(new SelectListItem() { Value = "0", Text = _localizationService.GetResource("Common.All") }); foreach (var m in manufacturers) model.AvailableManufacturers.Add(new SelectListItem() { Value = m.Id.ToString(), Text = m.Name, Selected = model.Mid == m.Id }); } IPagedList<Product> products = new PagedList<Product>(new List<Product>(), 0, 1); // only search if query string search keyword is set (used to avoid searching or displaying search term min length error message on /search page load) if (Request.Params["Q"] != null) { if (model.Q.Length < _catalogSettings.ProductSearchTermMinimumLength) { model.Warning = string.Format(_localizationService.GetResource("Search.SearchTermMinimumLengthIsNCharacters"), _catalogSettings.ProductSearchTermMinimumLength); } else { var categoryIds = new List<int>(); int manufacturerId = 0; decimal? minPriceConverted = null; decimal? maxPriceConverted = null; bool searchInDescriptions = false; if (model.As) { //advanced search var categoryId = model.Cid; if (categoryId > 0) { categoryIds.Add(categoryId); if (model.Isc) { //include subcategories categoryIds.AddRange(GetChildCategoryIds(categoryId)); } } manufacturerId = model.Mid; //min price if (!string.IsNullOrEmpty(model.Pf)) { decimal minPrice = decimal.Zero; if (decimal.TryParse(model.Pf, out minPrice)) minPriceConverted = _currencyService.ConvertToPrimaryStoreCurrency(minPrice, _workContext.WorkingCurrency); } //max price if (!string.IsNullOrEmpty(model.Pt)) { decimal maxPrice = decimal.Zero; if (decimal.TryParse(model.Pt, out maxPrice)) maxPriceConverted = _currencyService.ConvertToPrimaryStoreCurrency(maxPrice, _workContext.WorkingCurrency); } searchInDescriptions = model.Sid; } //products IList<int> filterableSpecificationAttributeOptionIds = null; products = _productService.SearchProducts(categoryIds, manufacturerId, null, minPriceConverted, maxPriceConverted, 0, model.Q, searchInDescriptions, _workContext.WorkingLanguage.Id, null, ProductSortingEnum.Position, command.PageNumber - 1, command.PageSize, false, out filterableSpecificationAttributeOptionIds); model.Products = products.Select(x => PrepareProductOverviewModel(x)).ToList(); model.NoResults = !model.Products.Any(); } } model.PagingFilteringContext.LoadPagedList(products); return View(model); }
private SearchModel GetCatalogSearhModel(SearchModel model, CatalogPagingFilteringModel command) { //var model = new SearchModel(); //string cacheKey = string.Format(ModelCacheEventConsumer.SEARCH_CATEGORIES_MODEL_KEY, //_workContext.WorkingLanguage.Id, //string.Join(",", _workContext.CurrentCustomer.GetCustomerRoleIds()), //_storeContext.CurrentStore.Id); var categoryIds = new List<int>(); var pvendorIds = new List<int>(); var customKeys = model.ss ?? new List<string>(); if (model.sms != null) customKeys.AddRange(model.sms); if (model.cid > 0) { categoryIds.Add(model.cid); var subcatids = _categoryService.GetAllCategoriesByParentCategoryId(model.cid).Select(c => c.Id).ToArray(); categoryIds.AddRange(subcatids); } var mdId = 0; decimal dsizeFrm = model.hm; decimal dsizeTo = model.hmx; PrepareViewModes(model.PagingFilteringContext, command); PreparePageSizeOptions(model.PagingFilteringContext, command, _catalogSettings.SearchPageAllowCustomersToSelectPageSize, _catalogSettings.SearchPagePageSizeOptions, _catalogSettings.SearchPageProductsPerPage); model.SelectedDimension = mdId; model.SizeFrom = dsizeFrm; model.SizeTo = dsizeTo; decimal sf = 0; decimal sst = 0; decimal varience = 10; var pgN = model.pg; var ipageSize = _catalogSettings.SearchPageProductsPerPage; if (command.PageSize == 0) command.PageSize = ipageSize; if (pgN == 0) command.PageNumber = 1; else command.PageNumber = pgN; var products = _productService.SearchProductsCustom( categoryIds: categoryIds, vendorIds: pvendorIds, customKeys: customKeys, sizeFrom: model.hm, sizeTo: model.hmx, varience: varience, storeId: _storeContext.CurrentStore.Id, visibleIndividuallyOnly: true, keywords:model.q, languageId: _workContext.WorkingLanguage.Id, orderBy: ProductSortingEnum.Position, pageIndex: command.PageNumber - 1, pageSize: command.PageSize, circaDateFrom:model.cdf ?? "", circaDateTo: model.cdt ?? "", color:model.c ?? "", designBy:model.d ?? "", widthFrom:model.wm, widthTo: model.wmx, priceMax:model.pmx, priceMin:model.pm); model.Products = PrepareProductOverviewModelsIB(products).OrderBy(p => p.Name).ToList(); model.PagingFilteringContext.LoadPagedList(products); return model; }
public ActionResult ShopByCategoryPaged(SearchModel model, CatalogPagingFilteringModel command) { model = GetCatalogSearhModel(model, command); return PartialView("_ProductList",model); }
//public ActionResult ShopByCategory(FormCollection f, CatalogPagingFilteringModel command) //{ // var model = new SearchModel(); // string cacheKey = string.Format(ModelCacheEventConsumer.SEARCH_CATEGORIES_MODEL_KEY, // _workContext.WorkingLanguage.Id, // string.Join(",", _workContext.CurrentCustomer.GetCustomerRoleIds()), // _storeContext.CurrentStore.Id); // var categories = _cacheManager.Get(cacheKey, () => // { // var categoriesModel = new List<SearchModel.CategoryModel>(); // all categories // var allCategories = _categoryService.GetAllCategories(); // top cats // return allCategories; // }); // model.AvailableCategories = (from c in categories // where (c.ParentCategoryId == 0) // select new SelectListItem { Text = c.Name, Value = c.Id.ToString() }).ToList(); // model.SubCategories = (from c in categories // where (c.ParentCategoryId != 0) // select new SelectListItem { Text = c.Name, Value = c.Id.ToString() }).ToList(); // foreach (var item in categories.Where(c => c.ParentCategoryId != 0)) // { // model.SubCategories.Add(new SelectListItem { Text = item.Name, Value = item.Id.ToString() }); // } // var vendors = _vendorService.GetAllVendors(); // model.Vendors = (from v in vendors // select new SelectListItem { Text = v.Name, Value = v.Id.ToString() }).ToList(); // CustomDataKeyGroupNames // var styles = _customDataService.GetCustomDataByKeyGroup(CustomDataKeyGroupNames.Style); // var circaDates = _customDataService.GetCustomDataByKeyGroup(CustomDataKeyGroupNames.CircaDate); // model.CustomData = ((from st in styles // select new SelectListItem { Text = st.Value, Value = st.Key }) // .Union // (from cd in circaDates // select new SelectListItem { Text = cd.Value, Value = cd.Key })).ToList(); // model.Dimension = _measureService.GetAllMeasureDimensions().Select(d => new SelectListItem { Text = d.Name, Value = d.Id.ToString() }).ToList(); // return View(model); //} //[HttpPost] public ActionResult ShopByCategory(string q="") { var model = new SearchModel(); //GetCatalogSearhModel(f, command);//new SearchModel(); string cacheKey = string.Format(ModelCacheEventConsumer.SEARCH_CATEGORIES_MODEL_KEY, _workContext.WorkingLanguage.Id, string.Join(",", _workContext.CurrentCustomer.GetCustomerRoleIds()), _storeContext.CurrentStore.Id); var categoryIds = new List<int>(); var pvendorIds = new List<int>(); var customKeys = new List<string>(); var categoriesModel = new List<SearchModel.CategoryModel>(); //all categories var categories = _categoryService.GetAllCategories(); model.ProductCategories = categories.ToList(); model.HeightMaxAvailable = _productService.MaxAvalableHeight(); model.HeightMinAvailable = _productService.MinAvalableHeight(); model.PriceMaxAvailable = _productService.MaxAvalablePrice(); model.PriceMinAvailable = _productService.MinAvalablePrice(); model.WidthMaxAvailable = _productService.MaxAvalableWidth(); model.WidthMinAvailable = _productService.MinAvalableWidth(); model.Styles = _customDataService.GetCustomDataByKeyGroup(CustomDataKeyGroupNames.Style).OrderBy(d => d.Key).ToList(); model.Materials = _customDataService.GetCustomDataByKeyGroup(CustomDataKeyGroupNames.Material).OrderBy(d=>d.Key).ToList(); model.Colors = _productService.GetColorList().Where(d => !string.IsNullOrWhiteSpace(d)).Select(d => new SelectListItem { Text = d, Value = d }).ToList(); model.Colors.Insert(0, new SelectListItem { Text = "-", Value = "" }); model.Designers = _productService.GetDesignerList().Where(d=> !string.IsNullOrWhiteSpace(d)).Select(d => new SelectListItem { Text = d, Value = d }).ToList(); model.Designers.Insert(0, new SelectListItem { Text = "-", Value = "" }); model.Dimension = _measureService.GetAllMeasureDimensions().Select(d => new SelectListItem { Text = d.Name, Value = d.Id.ToString() }).ToList(); model.q = ""; model.Currency = _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId).CurrencyCode; model.SystemDimention = _measureService.GetMeasureDimensionById(_measureSettings.BaseDimensionId).Name; var products = _productService.SearchProductsCustom( categoryIds: categoryIds, vendorIds: pvendorIds, customKeys: customKeys, sizeFrom: 0, sizeTo: 0, varience: 0, storeId: _storeContext.CurrentStore.Id, visibleIndividuallyOnly: true, keywords: q ??"", languageId: _workContext.WorkingLanguage.Id, orderBy: ProductSortingEnum.Position, pageIndex: 1, pageSize: _catalogSettings.SearchPageProductsPerPage); model.Products = PrepareProductOverviewModelsIB(products).OrderBy(p => p.Name).ToList(); model.PagingFilteringContext.LoadPagedList(products); return View(model); }
public ActionResult Search(SearchModel model, SearchPagingFilteringModel command) { if (model == null) model = new SearchModel(); //'Continue shopping' URL _genericAttributeService.SaveAttribute(_workContext.CurrentCustomer, SystemCustomerAttributeNames.LastContinueShoppingPage, _webHelper.GetThisPageUrl(false), _storeContext.CurrentStore.Id); if (command.PageSize <= 0) command.PageSize = _catalogSettings.SearchPageProductsPerPage; if (command.PageNumber <= 0) command.PageNumber = 1; if (model.Q == null) model.Q = ""; model.Q = model.Q.Trim(); var categories = _categoryService.GetAllCategories(); if (categories.Count > 0) { //first empty entry model.AvailableCategories.Add(new SelectListItem() { Value = "0", Text = _localizationService.GetResource("Common.All") }); //all other categories foreach (var c in categories) { //generate full category name (breadcrumb) string fullCategoryBreadcrumbName = ""; var breadcrumb = GetCategoryBreadCrumb(c); for (int i = 0; i <= breadcrumb.Count - 1; i++) { fullCategoryBreadcrumbName += breadcrumb[i].GetLocalized(x => x.Name); if (i != breadcrumb.Count - 1) fullCategoryBreadcrumbName += " >> "; } model.AvailableCategories.Add(new SelectListItem() { Value = c.Id.ToString(), Text = fullCategoryBreadcrumbName, Selected = model.Cid == c.Id }); } } var manufacturers = _manufacturerService.GetAllManufacturers(); if (manufacturers.Count > 0) { model.AvailableManufacturers.Add(new SelectListItem() { Value = "0", Text = _localizationService.GetResource("Common.All") }); foreach (var m in manufacturers) model.AvailableManufacturers.Add(new SelectListItem() { Value = m.Id.ToString(), Text = m.GetLocalized(x => x.Name), Selected = model.Mid == m.Id }); } IPagedList<Product> products = new PagedList<Product>(new List<Product>(), 0, 1); // only search if query string search keyword is set (used to avoid searching or displaying search term min length error message on /search page load) if (Request.Params["Q"] != null) { if (model.Q.Length < _catalogSettings.ProductSearchTermMinimumLength) { model.Warning = string.Format(_localizationService.GetResource("Search.SearchTermMinimumLengthIsNCharacters"), _catalogSettings.ProductSearchTermMinimumLength); } else { var categoryIds = new List<int>(); int manufacturerId = 0; decimal? minPriceConverted = null; decimal? maxPriceConverted = null; bool searchInDescriptions = false; if (model.As) { //advanced search var categoryId = model.Cid; if (categoryId > 0) { categoryIds.Add(categoryId); if (model.Isc) { //include subcategories categoryIds.AddRange(GetChildCategoryIds(categoryId)); } } manufacturerId = model.Mid; //min price if (!string.IsNullOrEmpty(model.Pf)) { decimal minPrice = decimal.Zero; if (decimal.TryParse(model.Pf, out minPrice)) minPriceConverted = _currencyService.ConvertToPrimaryStoreCurrency(minPrice, _workContext.WorkingCurrency); } //max price if (!string.IsNullOrEmpty(model.Pt)) { decimal maxPrice = decimal.Zero; if (decimal.TryParse(model.Pt, out maxPrice)) maxPriceConverted = _currencyService.ConvertToPrimaryStoreCurrency(maxPrice, _workContext.WorkingCurrency); } searchInDescriptions = model.Sid; } //var searchInProductTags = false; var searchInProductTags = searchInDescriptions; //products products = _productService.SearchProducts( categoryIds: categoryIds, manufacturerId: manufacturerId, storeId: _storeContext.CurrentStore.Id, priceMin: minPriceConverted, priceMax: maxPriceConverted, keywords:model.Q, searchDescriptions: searchInDescriptions, searchProductTags: searchInProductTags, languageId:_workContext.WorkingLanguage.Id, pageIndex: command.PageNumber - 1, pageSize: command.PageSize); model.Products = PrepareProductOverviewModels(products).ToList(); model.NoResults = !model.Products.Any(); //event _eventPublisher.Publish(new ProductSearchEvent() { SearchTerm = model.Q, SearchInDescriptions = searchInDescriptions, CategoryIds = categoryIds, ManufacturerId = manufacturerId, WorkingLanguageId = _workContext.WorkingLanguage.Id }); } } model.PagingFilteringContext.LoadPagedList(products); return View(model); }