如何在Nopcommerce 3.8中搜索中添加类别名称

时间:2016-10-04 07:34:12

标签: nopcommerce

您好,我想让客户输入类别名称并获取一些搜索结果。目前,当您键入类别名称时,它表示没有结果。

   public ActionResult AdvanceSearch(SearchModel model, CatalogPagingFilteringModel command)
    {
        //'Continue shopping' URL
        _genericAttributeService.SaveAttribute(_workContext.CurrentCustomer,
            SystemCustomerAttributeNames.LastContinueShoppingPage,
            _webHelper.GetThisPageUrl(false),
            _storeContext.CurrentStore.Id);

        if (model == null)
            model = new SearchModel();

        var searchTerms = model.q;
        if (searchTerms == null)
            searchTerms = "";
        searchTerms = searchTerms.Trim();



        //sorting
        PrepareSortingOptions(model.PagingFilteringContext, command);
        //view mode
        PrepareViewModes(model.PagingFilteringContext, command);
        //page size
        PreparePageSizeOptions(model.PagingFilteringContext, command,
            _catalogSettings.SearchPageAllowCustomersToSelectPageSize,
            _catalogSettings.SearchPagePageSizeOptions,
            _catalogSettings.SearchPageProductsPerPage);


        string cacheKey = string.Format(ModelCacheEventConsumer.SEARCH_CATEGORIES_MODEL_KEY,
            _workContext.WorkingLanguage.Id,
            string.Join(",", _workContext.CurrentCustomer.GetCustomerRoleIds()),
            _storeContext.CurrentStore.Id);
        var categories = _cacheManager.Get(cacheKey, () =>
        {
            var categoriesModel = new List<SearchModel.CategoryModel>();
            //all categories
            var allCategories = _categoryService.GetAllCategories(storeId: _storeContext.CurrentStore.Id);
            foreach (var c in allCategories)
            {
                //generate full category name (breadcrumb)
                string categoryBreadcrumb = "";
                var breadcrumb = c.GetCategoryBreadCrumb(allCategories, _aclService, _storeMappingService);
                for (int i = 0; i <= breadcrumb.Count - 1; i++)
                {
                    categoryBreadcrumb += breadcrumb[i].GetLocalized(x => x.Name);
                    if (i != breadcrumb.Count - 1)
                        categoryBreadcrumb += " >> ";
                }
                categoriesModel.Add(new SearchModel.CategoryModel
                {
                    Id = c.Id,
                    Breadcrumb = categoryBreadcrumb
                });
            }
            return categoriesModel;
        });
        if (categories.Any())
        {
            //first empty entry
            model.AvailableCategories.Add(new SelectListItem
            {
                Value = "0",
                Text = _localizationService.GetResource("Common.All")
            });
            //all other categories
            foreach (var c in categories)
            {
                model.AvailableCategories.Add(new SelectListItem
                {
                    Value = c.Id.ToString(),
                    Text = c.Breadcrumb,
                    Selected = model.cid == c.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 (searchTerms.Length < _catalogSettings.ProductSearchTermMinimumLength)
            {
                model.Warning = string.Format(_localizationService.GetResource("Search.SearchTermMinimumLengthIsNCharacters"), _catalogSettings.ProductSearchTermMinimumLength);
            }
            else
            {
                var categoryIds = new List<int>();
                int manufacturerId = 0;
                decimal? minPriceConverted = null;
                decimal? maxPriceConverted = null;
                bool searchInDescriptions = false;
                int vendorId = 0;
                if (model.adv)
                {
                    //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;
                        if (decimal.TryParse(model.pf, out minPrice))
                            minPriceConverted = _currencyService.ConvertToPrimaryStoreCurrency(minPrice, _workContext.WorkingCurrency);
                    }
                    //max price
                    if (!string.IsNullOrEmpty(model.pt))
                    {
                        decimal maxPrice;
                        if (decimal.TryParse(model.pt, out maxPrice))
                            maxPriceConverted = _currencyService.ConvertToPrimaryStoreCurrency(maxPrice, _workContext.WorkingCurrency);
                    }

                    if (model.asv)
                        vendorId = model.vid;

                    searchInDescriptions = model.sid;
                }

                //var searchInProductTags = false;
                var searchInProductTags = searchInDescriptions;

                //products
                products = _productService.SearchProducts(
                    categoryIds: categoryIds,
                    manufacturerId: manufacturerId,
                    storeId: _storeContext.CurrentStore.Id,
                    visibleIndividuallyOnly: true,
                    priceMin: minPriceConverted,
                    priceMax: maxPriceConverted,
                    keywords: searchTerms,
                    //searchDescriptions: searchInDescriptions,
                    searchProductTags: searchInProductTags,
                    searchSku: true,
                    languageId: _workContext.WorkingLanguage.Id,
                    orderBy: (ProductSortingEnum)command.OrderBy,
                    pageIndex: command.PageNumber - 1,
                    pageSize: command.PageSize,
                    vendorId: vendorId);
                model.Products = PrepareProductOverviewModels(products).ToList();

                model.NoResults = !model.Products.Any();

                //search term statistics
                if (!String.IsNullOrEmpty(searchTerms))
                {
                    var searchTerm = _searchTermService.GetSearchTermByKeyword(searchTerms, _storeContext.CurrentStore.Id);
                    if (searchTerm != null)
                    {
                        searchTerm.Count++;
                        _searchTermService.UpdateSearchTerm(searchTerm);
                    }
                    else
                    {
                        searchTerm = new SearchTerm
                        {
                            Keyword = searchTerms,
                            StoreId = _storeContext.CurrentStore.Id,
                            Count = 1
                        };
                        _searchTermService.InsertSearchTerm(searchTerm);
                    }
                }

                //event
                _eventPublisher.Publish(new ProductSearchEvent
                {
                    SearchTerm = searchTerms,
                    SearchInDescriptions = searchInDescriptions,
                    CategoryIds = categoryIds,
                    ManufacturerId = manufacturerId,
                    WorkingLanguageId = _workContext.WorkingLanguage.Id,
                    VendorId = vendorId
                });
            }
        }

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

公共行动方法&#39; AdvanceSearch&#39;在控制器&#39; Nop.Web.Controllers.CatalogController&#39;中找不到。

0 个答案:

没有答案