public ActionResult ProductsByTag(int productTagId, CatalogSearchQuery query)
        {
            var productTag = _productTagService.GetProductTagById(productTagId);

            if (productTag == null)
            {
                return(HttpNotFound());
            }

            var model = new ProductsByTagModel()
            {
                Id      = productTag.Id,
                TagName = productTag.GetLocalized(y => y.Name)
            };

            // Products
            query.WithProductTagIds(new int[] { productTagId });

            var searchResult = _catalogSearchService.Search(query);

            model.SearchResult = searchResult;

            var mappingSettings = _helper.GetBestFitProductSummaryMappingSettings(query.GetViewMode());

            model.Products = _helper.MapProductSummaryModel(searchResult.Hits, mappingSettings);

            // Prepare paging/sorting/mode stuff
            _helper.MapListActions(model.Products, null, _catalogSettings.DefaultPageSizeOptions);

            return(View(model));
        }
        public async Task <IActionResult> ProductsByTag(int productTagId, CatalogSearchQuery query)
        {
            var productTag = await _db.ProductTags.FindByIdAsync(productTagId, false);

            if (productTag == null)
            {
                return(NotFound());
            }

            if (!productTag.Published && !await Services.Permissions.AuthorizeAsync(Permissions.Catalog.Product.Read))
            {
                return(NotFound());
            }

            var model = new ProductsByTagModel
            {
                Id      = productTag.Id,
                TagName = productTag.GetLocalized(y => y.Name)
            };

            if (_seoSettings.CanonicalUrlsEnabled)
            {
                model.CanonicalUrl = _urlHelper.Value.RouteUrl("ProductsByTag", new { productTagId, path = model.TagName }, Request.Scheme);
            }

            query.WithProductTagIds(productTagId);

            var searchResult = await _catalogSearchService.SearchAsync(query);

            model.SearchResult = searchResult;

            var mappingSettings = _helper.GetBestFitProductSummaryMappingSettings(_helper.GetSearchQueryViewMode(query));

            model.Products = await _helper.MapProductSummaryModelAsync(searchResult, mappingSettings);

            // Prepare paging/sorting/mode stuff.
            _helper.MapListActions(model.Products, null, _catalogSettings.DefaultPageSizeOptions);

            return(View(model));
        }
Exemple #3
0
        public virtual ProductsByTagModel PrepareProductsByTagModel(ProductTag productTag, CatalogPagingFilteringModel command)
        {
            if (productTag == null)
            {
                throw new ArgumentNullException("productTag");
            }

            var model = new ProductsByTagModel
            {
                Id        = productTag.Id,
                TagName   = productTag.GetLocalized(y => y.Name),
                TagSeName = productTag.GetSeName()
            };


            //sorting
            PrepareSortingOptions(model.PagingFilteringContext, command);
            //view mode
            PrepareViewModes(model.PagingFilteringContext, command);
            //page size
            PreparePageSizeOptions(model.PagingFilteringContext, command,
                                   _catalogSettings.ProductsByTagAllowCustomersToSelectPageSize,
                                   _catalogSettings.ProductsByTagPageSizeOptions,
                                   _catalogSettings.ProductsByTagPageSize);


            //products
            var products = _productService.SearchProducts(
                storeId: _storeContext.CurrentStore.Id,
                productTagId: productTag.Id,
                visibleIndividuallyOnly: true,
                orderBy: (ProductSortingEnum)command.OrderBy,
                pageIndex: command.PageNumber - 1,
                pageSize: command.PageSize);

            model.Products = _productModelFactory.PrepareProductOverviewModels(products).ToList();

            model.PagingFilteringContext.LoadPagedList(products);
            return(model);
        }