protected virtual void ConvertPagingSorting(CatalogSearchQuery query, RouteData routeData, string origin) { var index = Math.Max(1, GetValueFor <int?>("i") ?? 1); var size = GetPageSize(query, routeData, origin); query.Slice((index - 1) * size, size); var orderBy = GetValueFor <ProductSortingEnum?>("o"); if (orderBy == null || orderBy == ProductSortingEnum.Initial) { if (origin.Equals("Search/Search")) { orderBy = _searchSettings.DefaultSortOrder; } else { orderBy = _catalogSettings.DefaultSortOrder; } } query.CustomData["CurrentSortOrder"] = orderBy.Value; query.SortBy(orderBy.Value); }
protected virtual async Task ConvertPagingSortingAsync(CatalogSearchQuery query, string origin) { var index = Math.Max(1, GetValueFor <int?>("i") ?? 1); var size = await GetPageSize(query, origin); query.Slice((index - 1) * size, size); var orderBy = GetValueFor <ProductSortingEnum?>("o"); if (orderBy == null || orderBy == ProductSortingEnum.Initial) { orderBy = origin.EqualsNoCase("Search/Search") ? _searchSettings.DefaultSortOrder : _catalogSettings.DefaultSortOrder; } query.CustomData["CurrentSortOrder"] = orderBy.Value; query.SortBy(orderBy.Value); }
public void LinqSearch_can_order_by_name() { var products = new List <Product>(); for (var i = 97; i <= 110; ++i) { products.Add(new SearchProduct(i) { Name = Convert.ToChar(i).ToString(), ShortDescription = "smart" }); } var query = new CatalogSearchQuery(new string[] { "shortdescription" }, "smart"); query.SortBy(ProductSortingEnum.NameDesc); var result = Search(query, products); Assert.That(string.Join(",", result.Hits.Select(x => x.Name)), Is.EqualTo("n,m,l,k,j,i,h,g,f,e,d,c,b,a")); }
public ActionResult RecentlyAddedProducts(CatalogSearchQuery query) { if (!_catalogSettings.RecentlyAddedProductsEnabled || _catalogSettings.RecentlyAddedProductsNumber <= 0) { return(View(ProductSummaryModel.Empty)); } query = query .SortBy(ProductSortingEnum.CreatedOn) .Slice(0, _catalogSettings.RecentlyAddedProductsNumber); var result = _catalogSearchService.Search(query); var settings = _helper.GetBestFitProductSummaryMappingSettings(query.GetViewMode()); var model = _helper.MapProductSummaryModel(result.Hits.ToList(), settings); model.GridColumnSpan = GridColumnSpan.Max5Cols; return(View(model)); }
public ActionResult RecentlyAddedProductsRss(CatalogSearchQuery query) { // TODO: (mc) find a more prominent place for the "NewProducts" link (may be in main menu?) var protocol = _services.WebHelper.IsCurrentConnectionSecured() ? "https" : "http"; var selfLink = Url.RouteUrl("RecentlyAddedProductsRSS", null, protocol); var recentProductsLink = Url.RouteUrl("RecentlyAddedProducts", null, protocol); var title = "{0} - {1}".FormatInvariant(_services.StoreContext.CurrentStore.Name, T("RSS.RecentlyAddedProducts")); var feed = new SmartSyndicationFeed(new Uri(recentProductsLink), title, T("RSS.InformationAboutProducts")); feed.AddNamespaces(true); feed.Init(selfLink, _services.WorkContext.WorkingLanguage); if (!_catalogSettings.RecentlyAddedProductsEnabled || _catalogSettings.RecentlyAddedProductsNumber <= 0) { return(new RssActionResult { Feed = feed }); } var items = new List <SyndicationItem>(); query.Sorting.Clear(); query = query .SortBy(ProductSortingEnum.CreatedOn) .Slice(0, _catalogSettings.RecentlyAddedProductsNumber); var result = _catalogSearchService.Search(query); var storeUrl = _services.StoreContext.CurrentStore.Url; foreach (var product in result.Hits) { string productUrl = Url.RouteUrl("Product", new { SeName = product.GetSeName() }, "http"); if (productUrl.HasValue()) { var item = feed.CreateItem( product.GetLocalized(x => x.Name), product.GetLocalized(x => x.ShortDescription), productUrl, product.CreatedOnUtc, product.FullDescription); try { // we add only the first picture var picture = product.ProductPictures.OrderBy(x => x.DisplayOrder).Select(x => x.Picture).FirstOrDefault(); if (picture != null) { feed.AddEnclosue(item, picture, _pictureService.GetPictureUrl(picture, _mediaSettings.ProductDetailsPictureSize, false, storeUrl)); } } catch { } items.Add(item); } } feed.Items = items; _services.DisplayControl.AnnounceRange(result.Hits); return(new RssActionResult { Feed = feed }); }
public async Task <IActionResult> ProductList(GridCommand command, ProductListModel model) { var gridModel = new GridModel <ProductOverviewModel>(); var fields = new List <string> { "name" }; if (_searchSettings.SearchFields.Contains("sku")) { fields.Add("sku"); } if (_searchSettings.SearchFields.Contains("shortdescription")) { fields.Add("shortdescription"); } var searchQuery = new CatalogSearchQuery(fields.ToArray(), model.SearchProductName) .HasStoreId(model.SearchStoreId) .WithCurrency(_workContext.WorkingCurrency) .WithLanguage(_workContext.WorkingLanguage); if (model.SearchIsPublished.HasValue) { searchQuery = searchQuery.PublishedOnly(model.SearchIsPublished.Value); } if (model.SearchHomePageProducts.HasValue) { searchQuery = searchQuery.HomePageProductsOnly(model.SearchHomePageProducts.Value); } if (model.SearchProductTypeId > 0) { searchQuery = searchQuery.IsProductType((ProductType)model.SearchProductTypeId); } if (model.SearchWithoutManufacturers.HasValue) { searchQuery = searchQuery.HasAnyManufacturer(!model.SearchWithoutManufacturers.Value); } else if (model.SearchManufacturerId != 0) { searchQuery = searchQuery.WithManufacturerIds(null, model.SearchManufacturerId); } if (model.SearchWithoutCategories.HasValue) { searchQuery = searchQuery.HasAnyCategory(!model.SearchWithoutCategories.Value); } else if (model.SearchCategoryId != 0) { searchQuery = searchQuery.WithCategoryIds(null, model.SearchCategoryId); } IPagedList <Product> products; if (_searchSettings.UseCatalogSearchInBackend) { searchQuery = searchQuery.Slice((command.Page - 1) * command.PageSize, command.PageSize); var sort = command.Sorting?.FirstOrDefault(); if (sort != null) { switch (sort.Member) { case nameof(ProductModel.Name): searchQuery = searchQuery.SortBy(sort.Descending ? ProductSortingEnum.NameDesc : ProductSortingEnum.NameAsc); break; case nameof(ProductModel.Price): searchQuery = searchQuery.SortBy(sort.Descending ? ProductSortingEnum.PriceDesc : ProductSortingEnum.PriceAsc); break; case nameof(ProductModel.CreatedOn): searchQuery = searchQuery.SortBy(sort.Descending ? ProductSortingEnum.CreatedOn : ProductSortingEnum.CreatedOnAsc); break; } } if (!searchQuery.Sorting.Any()) { searchQuery = searchQuery.SortBy(ProductSortingEnum.NameAsc); } var searchResult = await _catalogSearchService.SearchAsync(searchQuery); products = await searchResult.GetHitsAsync(); } else { var query = _catalogSearchService .PrepareQuery(searchQuery) .ApplyGridCommand(command, false); products = await new PagedList <Product>(query, command.Page - 1, command.PageSize).LoadAsync(); } var fileIds = products.AsEnumerable() .Select(x => x.MainPictureId ?? 0) .Where(x => x != 0) .Distinct() .ToArray(); var files = (await _mediaService.GetFilesByIdsAsync(fileIds)).ToDictionarySafe(x => x.Id); gridModel.Rows = products.AsEnumerable().Select(x => { var productModel = new ProductOverviewModel { Sku = x.Sku, Published = x.Published, ProductTypeLabelHint = x.ProductTypeLabelHint, Name = x.Name, Id = x.Id, StockQuantity = x.StockQuantity, Price = x.Price, LimitedToStores = x.LimitedToStores, EditUrl = Url.Action("Edit", "Product", new { id = x.Id }), ManufacturerPartNumber = x.ManufacturerPartNumber, Gtin = x.Gtin, MinStockQuantity = x.MinStockQuantity, OldPrice = x.OldPrice, AvailableStartDateTimeUtc = x.AvailableStartDateTimeUtc, AvailableEndDateTimeUtc = x.AvailableEndDateTimeUtc }; //MiniMapper.Map(x, productModel); files.TryGetValue(x.MainPictureId ?? 0, out var file); // TODO: (core) Use IImageModel productModel.PictureThumbnailUrl = _mediaService.GetUrl(file, _mediaSettings.CartThumbPictureSize); productModel.NoThumb = file == null; productModel.ProductTypeName = x.GetProductTypeLabel(_localizationService); productModel.UpdatedOn = _dateTimeHelper.ConvertToUserTime(x.UpdatedOnUtc, DateTimeKind.Utc); productModel.CreatedOn = _dateTimeHelper.ConvertToUserTime(x.CreatedOnUtc, DateTimeKind.Utc); return(productModel); }); gridModel.Total = products.TotalCount; return(Json(gridModel)); }