public ActionResult CategoryNavigation(int currentCategoryId, int currentProductId) { //get active category int activeCategoryId = 0; if (currentCategoryId > 0) { //category details page activeCategoryId = currentCategoryId; } else if (currentProductId > 0) { //product details page var productCategories = _categoryService.GetProductCategoriesByProductId(currentProductId); if (productCategories.Count > 0) activeCategoryId = productCategories[0].CategoryId; } var customerRolesIds = _workContext.CurrentCustomer.CustomerRoles .Where(cr => cr.Active).Select(cr => cr.Id).ToList(); string cacheKey = string.Format(ModelCacheEventConsumer.CATEGORY_NAVIGATION_MODEL_KEY, _workContext.WorkingLanguage.Id, string.Join(",", customerRolesIds), _storeContext.CurrentStore.Id, activeCategoryId); var cachedModel = _cacheManager.Get(cacheKey, () => { var activeCategory = _categoryService.GetCategoryById(activeCategoryId); var breadCrumb = activeCategory != null ? activeCategory.GetCategoryBreadCrumb(_categoryService, _aclService, _storeMappingService) .Select(x => x.Id).ToList() : new List<int>(); return PrepareCategorySimpleModels(0, breadCrumb, 0, int.MaxValue, false).ToList(); }); var model = new CategoryNavigationModel() { CurrentCategoryId = activeCategoryId, Categories = cachedModel }; return PartialView(model); }
public ActionResult CategoryNavigation(int currentCategoryId, int currentProductId) { //get active category int activeCategoryId = 0; if (currentCategoryId > 0) { //category details page activeCategoryId = currentCategoryId; } else if (currentProductId > 0) { //product details page var productCategories = _categoryService.GetProductCategoriesByProductId(currentProductId); if (productCategories.Count > 0) activeCategoryId = productCategories[0].CategoryId; } string cacheKey = string.Format(ModelCacheEventConsumer.CATEGORY_NAVIGATION_MODEL_KEY, _workContext.WorkingLanguage.Id, string.Join(",", _workContext.CurrentCustomer.GetCustomerRoleIds()), _storeContext.CurrentStore.Id); var cachedModel = _cacheManager.Get(cacheKey, () => PrepareCategorySimpleModels(0).ToList()); var model = new CategoryNavigationModel { CurrentCategoryId = activeCategoryId, Categories = cachedModel }; return PartialView(model); }
public ActionResult CategoryNavigation(int currentCategoryId, int currentProductId) { var customerRolesIds = _workContext.CurrentCustomer.CustomerRoles .Where(cr => cr.Active).Select(cr => cr.Id).ToList(); string cacheKey = string.Format(ModelCacheEventConsumer.CATEGORY_NAVIGATION_MODEL_KEY, currentCategoryId, currentProductId, _workContext.WorkingLanguage.Id, string.Join(",", customerRolesIds)); var cacheModel = _cacheManager.Get(cacheKey, () => { //get current category var currentCategory = _categoryService.GetCategoryById(currentCategoryId); if (currentCategory == null && currentProductId > 0) { var productCategories = _categoryService.GetProductCategoriesByProductId(currentProductId); if (productCategories.Count > 0) currentCategory = productCategories[0].Category; } //prepare model var model = new CategoryNavigationModel(); model.CurrentCategoryId = currentCategory != null ? currentCategory.Id : 0; var breadCrumb = currentCategory != null ? GetCategoryBreadCrumb(currentCategory) : new List<Category>(); model.Categories.AddRange(PrepareCategoryNavigationModel(breadCrumb, 0, 0)); return model; } ); return PartialView(cacheModel); }
protected IList<CategoryNavigationModel> GetChildCategoryNavigationModel(IList<Category> breadCrumb, int rootCategoryId, Category currentCategory, int level) { var result = new List<CategoryNavigationModel>(); foreach (var category in _categoryService.GetAllCategoriesByParentCategoryId(rootCategoryId)) { var model = new CategoryNavigationModel() { Id = category.Id, Name = category.GetLocalized(x => x.Name), SeName = category.GetSeName(), IsActive = currentCategory != null && currentCategory.Id == category.Id, NumberOfParentCategories = level }; if (_catalogSettings.ShowCategoryProductNumber) { model.DisplayNumberOfProducts = true; var categoryIds = new List<int>(); categoryIds.Add(category.Id); if (_catalogSettings.ShowCategoryProductNumberIncludingSubcategories) { //include subcategories categoryIds.AddRange(GetChildCategoryIds(category.Id)); } IList<int> filterableSpecificationAttributeOptionIds = null; model.NumberOfProducts = _productService.SearchProducts(categoryIds, 0, null, null, null, 0, string.Empty, false, 0, null, ProductSortingEnum.Position, 0, 1, false, out filterableSpecificationAttributeOptionIds).TotalCount; } result.Add(model); // Comento las siguientes dos líneas para mostrar tanto Categorías como Subcategorias //for (int i = 0; i <= breadCrumb.Count - 1; i++) // if (breadCrumb[i].Id == category.Id) result.AddRange(GetChildCategoryNavigationModel(breadCrumb, category.Id, currentCategory, level + 1)); } return result; }
protected IList<CategoryNavigationModel> GetChildCategoryNavigationModel(IList<Category> breadCrumb, int rootCategoryId, Category currentCategory, int level) { var result = new List<CategoryNavigationModel>(); foreach (var category in _categoryService.GetAllCategoriesByParentCategoryId(rootCategoryId)) { var model = new CategoryNavigationModel() { Id = category.Id, Name = category.GetLocalized(x => x.Name), SeName = category.GetSeName(), IsActive = currentCategory != null && currentCategory.Id == category.Id, NumberOfParentCategories = level }; if (_catalogSettings.ShowCategoryProductNumber) { model.DisplayNumberOfProducts = true; model.NumberOfProducts = GetNumberOfProducts(category, _catalogSettings.ShowCategoryProductNumberIncludingSubcategories); } result.Add(model); for (int i = 0; i <= breadCrumb.Count - 1; i++) if (breadCrumb[i].Id == category.Id) result.AddRange(GetChildCategoryNavigationModel(breadCrumb, category.Id, currentCategory, level + 1)); } return result; }