public async Task CanPreparePageSizeOptions() { var pageSizes = "10, 20, 30"; var model = new CatalogProductsModel(); var command = new CatalogProductsCommand(); await _catalogModelFactory.PreparePageSizeOptionsAsync(model, command, true, pageSizes, 0); model.AllowCustomersToSelectPageSize.Should().BeTrue(); model.PageSizeOptions.Count.Should().Be(3); foreach (var modelPageSizeOption in model.PageSizeOptions) { int.TryParse(modelPageSizeOption.Text, out _).Should().BeTrue(); pageSizes.Contains(modelPageSizeOption.Text).Should().BeTrue(); modelPageSizeOption.Value.Should().Be(modelPageSizeOption.Text); } command.PageSize.Should().Be(10); await _catalogModelFactory.PreparePageSizeOptionsAsync(model, command, false, "10, 20, 30", 15); model.AllowCustomersToSelectPageSize.Should().BeFalse(); command.PageSize.Should().Be(15); model.PageSizeOptions.Count.Should().Be(3); }
public virtual async Task <IActionResult> Category(int categoryId, CatalogProductsCommand command) { var category = await _categoryService.GetCategoryByIdAsync(categoryId); if (!await CheckCategoryAvailabilityAsync(category)) { return(InvokeHttp404()); } //'Continue shopping' URL await _genericAttributeService.SaveAttributeAsync(await _workContext.GetCurrentCustomerAsync(), NopCustomerDefaults.LastContinueShoppingPageAttribute, _webHelper.GetThisPageUrl(false), (await _storeContext.GetCurrentStoreAsync()).Id); //display "edit" (manage) link if (await _permissionService.AuthorizeAsync(StandardPermissionProvider.AccessAdminPanel) && await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManageCategories)) { DisplayEditLink(Url.Action("Edit", "Category", new { id = category.Id, area = AreaNames.Admin })); } //activity log await _customerActivityService.InsertActivityAsync("PublicStore.ViewCategory", string.Format(await _localizationService.GetResourceAsync("ActivityLog.PublicStore.ViewCategory"), category.Name), category); //model var model = await _catalogModelFactory.PrepareCategoryModelAsync(category, command); //template var templateViewPath = await _catalogModelFactory.PrepareCategoryTemplateViewPathAsync(category.CategoryTemplateId); return(View(templateViewPath, model)); }
public virtual async Task <IActionResult> Vendor(int vendorId, CatalogProductsCommand command) { var vendor = await _vendorService.GetVendorByIdAsync(vendorId); if (!await CheckVendorAvailabilityAsync(vendor)) { return(InvokeHttp404()); } //'Continue shopping' URL await _genericAttributeService.SaveAttributeAsync(await _workContext.GetCurrentCustomerAsync(), NopCustomerDefaults.LastContinueShoppingPageAttribute, _webHelper.GetThisPageUrl(false), (await _storeContext.GetCurrentStoreAsync()).Id); //display "edit" (manage) link if (await _permissionService.AuthorizeAsync(StandardPermissionProvider.AccessAdminPanel) && await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManageVendors)) { DisplayEditLink(Url.Action("Edit", "Vendor", new { id = vendor.Id, area = AreaNames.Admin })); } //model var model = await _catalogModelFactory.PrepareVendorModelAsync(vendor, command); return(View(model)); }
public async Task CanPrepareSortingOptions() { var model = new CatalogProductsModel(); var command = new CatalogProductsCommand(); await _catalogModelFactory.PrepareSortingOptionsAsync(model, command); model.AllowProductSorting.Should().BeTrue(); model.AvailableSortOptions.Count.Should().Be(6); command.OrderBy.Should().Be(0); }
private List <Product> SortPromoProducts( IList <Product> promoProducts, CatalogProductsCommand command ) { if (command.OrderBy == 11) { return(promoProducts.OrderByDescending(p => p.Price).ToList()); } return(promoProducts.OrderBy(p => p.Price).ToList()); }
public virtual async Task <IActionResult> GetTagProducts(int tagId, CatalogProductsCommand command) { var productTag = await _productTagService.GetProductTagByIdAsync(tagId); if (productTag == null) { return(NotFound()); } var model = await _catalogModelFactory.PrepareTagProductsModelAsync(productTag, command); return(PartialView("_ProductsInGridOrLines", model)); }
public virtual async Task <IActionResult> ProductsByTag(int productTagId, CatalogProductsCommand command) { var productTag = await _productTagService.GetProductTagByIdAsync(productTagId); if (productTag == null) { return(InvokeHttp404()); } var model = await _catalogModelFactory.PrepareProductsByTagModelAsync(productTag, command); return(View(model)); }
public virtual async Task <IActionResult> GetVendorProducts(int vendorId, CatalogProductsCommand command) { var vendor = await _vendorService.GetVendorByIdAsync(vendorId); if (!await CheckVendorAvailabilityAsync(vendor)) { return(NotFound()); } var model = await _catalogModelFactory.PrepareVendorProductsModelAsync(vendor, command); return(PartialView("_ProductsInGridOrLines", model)); }
public virtual async Task <IActionResult> Search(SearchModel model, CatalogProductsCommand command) { //'Continue shopping' URL await _genericAttributeService.SaveAttributeAsync(await _workContext.GetCurrentCustomerAsync(), NopCustomerDefaults.LastContinueShoppingPageAttribute, _webHelper.GetThisPageUrl(true), (await _storeContext.GetCurrentStoreAsync()).Id); if (model == null) { model = new SearchModel(); } model = await _catalogModelFactory.PrepareSearchModelAsync(model, command); return(View(model)); }
private async Task PrepareSortingOptionsAsync(PromoListingModel model, CatalogProductsCommand command) { //set the order by position by default model.OrderBy = command.OrderBy; command.OrderBy = (int)ProductSortingEnum.Position; //ensure that product sorting is enabled if (!_catalogSettings.AllowProductSorting) { return; } //get active sorting options var activeSortingOptionsIds = Enum.GetValues(typeof(ProductSortingEnum)).Cast <int>() .Except(_catalogSettings.ProductSortingEnumDisabled).ToList(); if (!activeSortingOptionsIds.Any()) { return; } //order sorting options var orderedActiveSortingOptions = activeSortingOptionsIds .Select(id => new { Id = id, Order = _catalogSettings.ProductSortingEnumDisplayOrder.TryGetValue(id, out var order) ? order : id })
public virtual async Task <IActionResult> SearchProducts(SearchModel searchModel, CatalogProductsCommand command) { if (searchModel == null) { searchModel = new SearchModel(); } var model = await _catalogModelFactory.PrepareSearchProductsModelAsync(searchModel, command); return(PartialView("_ProductsInGridOrLines", model)); }
public virtual async Task <IActionResult> GetManufacturerProducts(int manufacturerId, CatalogProductsCommand command) { var manufacturer = await _manufacturerService.GetManufacturerByIdAsync(manufacturerId); if (!await CheckManufacturerAvailabilityAsync(manufacturer)) { return(NotFound()); } var model = await _catalogModelFactory.PrepareManufacturerProductsModelAsync(manufacturer, command); return(PartialView("_ProductsInGridOrLines", model)); }
public virtual async Task <IActionResult> GetCategoryProducts(int categoryId, CatalogProductsCommand command) { var category = await _categoryService.GetCategoryByIdAsync(categoryId); if (!await CheckCategoryAvailabilityAsync(category)) { return(NotFound()); } var model = await _catalogModelFactory.PrepareCategoryProductsModelAsync(category, command); return(PartialView("_ProductsInGridOrLines", model)); }
public async Task <IActionResult> Promo(string promoSlug, CatalogProductsCommand command) { var urlRecord = await _urlRecordService.GetBySlugAsync(promoSlug); if (urlRecord == null) { return(InvokeHttp404()); } var promo = await _abcPromoService.GetPromoByIdAsync(urlRecord.EntityId); if (promo == null) { return(InvokeHttp404()); } var shouldDisplay = _settings.IncludeExpiredPromosOnRebatesPromosPage ? promo.IsExpired() || promo.IsActive() : promo.IsActive(); if (!shouldDisplay) { return(InvokeHttp404()); } var promoProducts = await _abcPromoService.GetPublishedProductsByPromoIdAsync(promo.Id); // if a category is provided, filter by it var filterCategory = await GetFilterCategoryAsync(); if (filterCategory != null) { var filterCategoryIds = (await _categoryService.GetChildCategoryIdsAsync(filterCategory.Id)).ToList(); filterCategoryIds.Add(filterCategory.Id); var categoryFilteredProducts = new List <Product>(); foreach (var product in promoProducts) { var pcs = await _categoryService.GetProductCategoriesByProductIdAsync(product.Id); var pcsCategoryIds = pcs.Select(pc => pc.CategoryId); var pcCategoryIdsWithParents = new List <int>(); if (pcsCategoryIds.Intersect(filterCategoryIds).Any()) { categoryFilteredProducts.Add(product); } } promoProducts = categoryFilteredProducts; } promoProducts = SortPromoProducts(promoProducts, command); var filteredPromoProducts = promoProducts.Skip(command.PageIndex * 20).Take(20).ToList(); var model = new PromoListingModel { Name = promo.ManufacturerId != null ? $"{(await _manufacturerService.GetManufacturerByIdAsync(promo.ManufacturerId.Value)).Name} - {promo.Description}" : promo.Description, Products = (await _productModelFactory.PrepareProductOverviewModelsAsync(filteredPromoProducts)).ToList(), BannerImageUrl = await promo.GetPromoBannerUrlAsync(), PromoFormPopup = promo.GetPopupCommand() }; var pagedList = new PagedList <Product>( filteredPromoProducts, command.PageIndex, 20, promoProducts.Count ); model.LoadPagedList(pagedList); // using duplicate sorting - it would be good to link this to NOP code but it's pretty complex await PrepareSortingOptionsAsync(model, command); return(View("~/Plugins/Widgets.AbcPromos/Views/PromoListing.cshtml", model)); }
public override async Task <CatalogProductsModel> PrepareSearchProductsModelAsync(SearchModel searchModel, CatalogProductsCommand command) { if (command == null) { throw new ArgumentNullException(nameof(command)); } var model = new CatalogProductsModel { UseAjaxLoading = _catalogSettings.UseAjaxCatalogProductsLoading }; //sorting await PrepareSortingOptionsAsync(model, command); //view mode await PrepareViewModesAsync(model, command); //page size await PreparePageSizeOptionsAsync(model, command, _catalogSettings.SearchPageAllowCustomersToSelectPageSize, _catalogSettings.SearchPagePageSizeOptions, _catalogSettings.SearchPageProductsPerPage); var searchTerms = searchModel.q == null ? string.Empty : searchModel.q.Trim(); IPagedList <Product> products = new PagedList <Product>(new List <Product>(), 0, 1); // only search if query string search keyword is set (used to aasync Task searching or displaying search term min length error message on /search page load) //we don't use "!string.IsNullOrEmpty(searchTerms)" in cases of "ProductSearchTermMinimumLength" set to 0 but searching by other parameters (e.g. category or price filter) var isSearchTermSpecified = _httpContextAccessor.HttpContext.Request.Query.ContainsKey("q"); if (isSearchTermSpecified) { var currentStore = await _storeContext.GetCurrentStoreAsync(); if (searchTerms.Length < _catalogSettings.ProductSearchTermMinimumLength) { model.WarningMessage = string.Format(await _localizationService.GetResourceAsync("Search.SearchTermMinimumLengthIsNCharacters"), _catalogSettings.ProductSearchTermMinimumLength); } else { var categoryIds = new List <int>(); var manufacturerId = 0; var searchInDescriptions = false; var vendorId = 0; if (searchModel.advs) { //advanced search var categoryId = searchModel.cid; if (categoryId > 0) { categoryIds.Add(categoryId); if (searchModel.isc) { //include subcategories categoryIds.AddRange( await _categoryService.GetChildCategoryIdsAsync(categoryId, currentStore.Id)); } } manufacturerId = searchModel.mid; if (searchModel.asv) { vendorId = searchModel.vid; } searchInDescriptions = searchModel.sid; } //var searchInProductTags = false; var searchInProductTags = searchInDescriptions; var workingLanguage = await _workContext.GetWorkingLanguageAsync(); //price range PriceRangeModel selectedPriceRange = null; if (_catalogSettings.EnablePriceRangeFiltering && _catalogSettings.SearchPagePriceRangeFiltering) { selectedPriceRange = await GetConvertedPriceRangeAsync(command); PriceRangeModel availablePriceRange = null; if (!_catalogSettings.SearchPageManuallyPriceRange) { async Task <decimal?> getProductPriceAsync(ProductSortingEnum orderBy) { var products = await _productService.SearchProductsAsync(0, 1, categoryIds : categoryIds, manufacturerIds : new List <int> { manufacturerId }, storeId : currentStore.Id, visibleIndividuallyOnly : true, keywords : searchTerms, searchDescriptions : searchInDescriptions, searchProductTags : searchInProductTags, languageId : workingLanguage.Id, vendorId : vendorId, orderBy : orderBy); return(products?.FirstOrDefault()?.Price ?? 0); } availablePriceRange = new PriceRangeModel { From = await getProductPriceAsync(ProductSortingEnum.PriceAsc), To = await getProductPriceAsync(ProductSortingEnum.PriceDesc) }; } else { availablePriceRange = new PriceRangeModel { From = _catalogSettings.SearchPagePriceFrom, To = _catalogSettings.SearchPagePriceTo }; } model.PriceRangeFilter = await PreparePriceRangeFilterAsync(selectedPriceRange, availablePriceRange); } //products if (_luceneSettings.Enabled) { _luceneService.GetLuceneDirectory(); var luceneProducts = _luceneService.Search(searchTerms); products = await luceneProducts.AsQueryable().ToPagedListAsync(command.PageNumber - 1, command.PageSize); } else { products = await _productService.SearchProductsAsync( command.PageNumber - 1, command.PageSize, categoryIds : categoryIds, manufacturerIds : new List <int> { manufacturerId }, storeId : currentStore.Id, visibleIndividuallyOnly : true, keywords : searchTerms, priceMin : selectedPriceRange?.From, priceMax : selectedPriceRange?.To, searchDescriptions : searchInDescriptions, searchProductTags : searchInProductTags, languageId : workingLanguage.Id, orderBy : (ProductSortingEnum)command.OrderBy, vendorId : vendorId); } //search term statistics if (!string.IsNullOrEmpty(searchTerms)) { var searchTerm = await _searchTermService.GetSearchTermByKeywordAsync(searchTerms, currentStore.Id); if (searchTerm != null) { searchTerm.Count++; await _searchTermService.UpdateSearchTermAsync(searchTerm); } else { searchTerm = new SearchTerm { Keyword = searchTerms, StoreId = currentStore.Id, Count = 1 }; await _searchTermService.InsertSearchTermAsync(searchTerm); } } //event await _eventPublisher.PublishAsync(new ProductSearchEvent { SearchTerm = searchTerms, SearchInDescriptions = searchInDescriptions, CategoryIds = categoryIds, ManufacturerId = manufacturerId, WorkingLanguageId = workingLanguage.Id, VendorId = vendorId }); } } var isFiltering = !string.IsNullOrEmpty(searchTerms); await PrepareCatalogProductsAsync(model, products, isFiltering); return(model); }