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 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)); }