public ActionResult Search(SearchModel model, SearchPagingFilteringModel command) { if (model == null) model = new SearchModel(); // 'Continue shopping' URL _genericAttributeService.SaveAttribute(_services.WorkContext.CurrentCustomer, SystemCustomerAttributeNames.LastContinueShoppingPage, _services.WebHelper.GetThisPageUrl(false), _services.StoreContext.CurrentStore.Id); if (command.PageSize <= 0) command.PageSize = _catalogSettings.SearchPageProductsPerPage; if (command.PageNumber <= 0) command.PageNumber = 1; if (command.OrderBy == (int)ProductSortingEnum.Initial) { command.OrderBy = (int)_catalogSettings.DefaultSortOrder; } _helper.PreparePagingFilteringModel(model.PagingFilteringContext, command, new PageSizeContext { AllowCustomersToSelectPageSize = _catalogSettings.ProductSearchAllowCustomersToSelectPageSize, PageSize = _catalogSettings.SearchPageProductsPerPage, PageSizeOptions = _catalogSettings.ProductSearchPageSizeOptions }); if (model.Q == null) model.Q = ""; model.Q = model.Q.Trim(); // Build AvailableCategories // first empty entry model.AvailableCategories.Add(new SelectListItem { Value = "0", Text = T("Common.All") }); var navModel = _helper.PrepareCategoryNavigationModel(0, 0); navModel.Root.TraverseTree((node) => { if (node.IsRoot) return; int id = node.Value.EntityId; var breadcrumb = node.GetBreadcrumb().Select(x => x.Text).ToArray(); model.AvailableCategories.Add(new SelectListItem { Value = id.ToString(), Text = String.Join(" > ", breadcrumb), Selected = model.Cid == id }); }); var manufacturers = _manufacturerService.GetAllManufacturers(); if (manufacturers.Count > 0) { model.AvailableManufacturers.Add(new SelectListItem { Value = "0", Text = T("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(T("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(_helper.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, _services.WorkContext.WorkingCurrency); } // max price if (!string.IsNullOrEmpty(model.Pt)) { decimal maxPrice = decimal.Zero; if (decimal.TryParse(model.Pt, out maxPrice)) maxPriceConverted = _currencyService.ConvertToPrimaryStoreCurrency(maxPrice, _services.WorkContext.WorkingCurrency); } searchInDescriptions = model.Sid; } //var searchInProductTags = false; var searchInProductTags = searchInDescriptions; //products var ctx = new ProductSearchContext(); ctx.CategoryIds = categoryIds; ctx.ManufacturerId = manufacturerId; ctx.PriceMin = minPriceConverted; ctx.PriceMax = maxPriceConverted; ctx.Keywords = model.Q; ctx.SearchDescriptions = searchInDescriptions; ctx.SearchSku = !_catalogSettings.SuppressSkuSearch; ctx.SearchProductTags = searchInProductTags; ctx.LanguageId = _services.WorkContext.WorkingLanguage.Id; ctx.OrderBy = (ProductSortingEnum)command.OrderBy; // ProductSortingEnum.Position; ctx.PageIndex = command.PageNumber - 1; ctx.PageSize = command.PageSize; ctx.StoreId = _services.StoreContext.CurrentStoreIdIfMultiStoreMode; ctx.VisibleIndividuallyOnly = true; products = _productService.SearchProducts(ctx); model.Products = _helper.PrepareProductOverviewModels( products, prepareColorAttributes: true, prepareManufacturers: command.ViewMode.IsCaseInsensitiveEqual("list")).ToList(); model.NoResults = !model.Products.Any(); } } 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; //_catalogSettings.SearchPageProductsPerPage; if (command.PageNumber <= 0) command.PageNumber = 1; // codehint: sm-edit PreparePagingFilteringModel(model.PagingFilteringContext, command, new PageSizeContext { AllowCustomersToSelectPageSize = _catalogSettings.ProductSearchAllowCustomersToSelectPageSize, PageSize = _catalogSettings.SearchPageProductsPerPage, PageSizeOptions = _catalogSettings.ProductSearchPageSizeOptions }); // codehint: sm-edit if (model.Q == null) model.Q = ""; model.Q = model.Q.Trim(); // Build AvailableCategories // first empty entry model.AvailableCategories.Add(new SelectListItem() { Value = "0", Text = T("Common.All") }); var navModel = GetCategoryNavigationModel(0, 0); navModel.Root.TraverseTree((node) => { if (node.IsRoot) return; int id = node.Value.Id; var breadcrumb = new List<string>(); while (node != null && !node.IsRoot) { breadcrumb.Add(node.Value.Name); node = node.Parent; } breadcrumb.Reverse(); model.AvailableCategories.Add(new SelectListItem() { Value = id.ToString(), Text = String.Join(" > ", breadcrumb), Selected = model.Cid == id }); }); #region Obsolete //var categories = _categoryService.GetAllCategories(); //// Perf! //var mappedCatgories = categories.ToDictionary(x => x.Id); //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, mappedCatgories); // 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 // }); // } //} #endregion var manufacturers = _manufacturerService.GetAllManufacturers(); if (manufacturers.Count > 0) { model.AvailableManufacturers.Add(new SelectListItem() { Value = "0", Text = T("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(T("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 var ctx = new ProductSearchContext(); ctx.CategoryIds = categoryIds; ctx.ManufacturerId = manufacturerId; ctx.PriceMin = minPriceConverted; ctx.PriceMax = maxPriceConverted; ctx.Keywords = model.Q; ctx.SearchDescriptions = searchInDescriptions; ctx.SearchSku = !_catalogSettings.SuppressSkuSearch; ctx.SearchProductTags = searchInProductTags; ctx.LanguageId = _workContext.WorkingLanguage.Id; ctx.OrderBy = (ProductSortingEnum)command.OrderBy; // ProductSortingEnum.Position; // codehint: sm-edit ctx.PageIndex = command.PageNumber - 1; ctx.PageSize = command.PageSize; ctx.StoreId = _storeContext.CurrentStoreIdIfMultiStoreMode; ctx.VisibleIndividuallyOnly = true; products = _productService.SearchProducts(ctx); model.Products = PrepareProductOverviewModels(products, prepareColorAttributes: true).ToList(); model.NoResults = !model.Products.Any(); } } model.PagingFilteringContext.LoadPagedList(products); return View(model); }