//[ResponseCache(Duration = 3600, VaryByQueryKeys = new string[] { "*" })]
        //public ActionResult CategoryNavigation(string widgetZone, object additionalData = null)
        //{
        //    //load settings for a chosen store scope
        //    var storeScope = _storeContext.CurrentStore.Id;
        //    var categoryNavigationSettings = _settingService.LoadSetting<CategoryNavigationSettings>(storeScope);
        //    int currentCategoryId = 0;
        //    int currentProductId = 0;
        //    if (additionalData != null)
        //    {
        //        string[] data = additionalData.ToString().Split(',');
        //        if (data.Any())
        //        {
        //            int.TryParse(data[0], out currentCategoryId);
        //            if (data.Count() > 1)
        //            {
        //                int.TryParse(data[1], out currentProductId);
        //            }
        //        }
        //    }
        //    //if (categoryNavigationSettings.IsActive)
        //    //{
        //        var model = _catalogModelFactoryCustom.PrepareCategoryNavigationModel(currentCategoryId, currentProductId);
        //        return View("~/Plugins/Catalog.GBS/Views/CategoryNavigationCustom.cshtml", model);
        //    //}
        //    //else
        //    //{
        //    //    var model = _catalogModelFactory.PrepareCategoryNavigationModel(currentCategoryId, currentProductId);
        //    //    return View("~/Plugins/Catalog.GBS/Views/CategoryNavigation.cshtml", model);
        //    //}
        //}

        public static bool HasSubcategoryProducts(CategorySimpleModelCustom category)
        {
            if (category.SubCategories.Any())
            {
                foreach (var subcategory in category.SubCategories)
                {
                    if (subcategory.ProductsCount > 0)
                    {
                        return(true);
                    }
                    else
                    {
                        return(HasSubcategoryProducts(subcategory));
                    }
                }
            }
            return(false);
        }
Esempio n. 2
0
        /// <summary>
        /// Prepare category (simple) models
        /// </summary>
        /// <param name="rootCategoryId">Root category identifier</param>
        /// <param name="loadSubCategories">A value indicating whether subcategories should be loaded</param>
        /// <param name="allCategories">All available categories; pass null to load them internally</param>
        /// <returns>List of category (simple) models</returns>
        public virtual List <CategorySimpleModelCustom> PrepareCategorySimpleModels(int rootCategoryId,
                                                                                    bool loadSubCategories = true, IList <Category> allCategories = null)
        {
            var result = new List <CategorySimpleModelCustom>();

            //little hack for performance optimization.
            //we know that this method is used to load top and left menu for categories.
            //it'll load all categories anyway.
            //so there's no need to invoke "GetAllCategoriesByParentCategoryId" multiple times (extra SQL commands) to load childs
            //so we load all categories at once
            //if you don't like this implementation if you can uncomment the line below (old behavior) and comment several next lines (before foreach)
            //var categories = _categoryService.GetAllCategoriesByParentCategoryId(rootCategoryId);
            if (allCategories == null)
            {
                //load categories if null passed
                //we implemeneted it this way for performance optimization - recursive iterations (below)
                //this way all categories are loaded only once
                allCategories = _categoryService.GetAllCategories(storeId: _storeContext.CurrentStore.Id);
            }
            var        categories = allCategories.Where(c => c.ParentCategoryId == rootCategoryId).ToList();
            List <int> BlackList  = new List <int>();

            try
            {
                if (!string.IsNullOrEmpty(_categoryNavigationSettings.BlackList))
                {
                    BlackList = _categoryNavigationSettings.BlackList.Split(',').Select(int.Parse).ToList();
                }
            }
            catch (Exception ex)
            {
                _logger.Error("BlackList misconfigured in Catalog Plugin - ", ex);
            }
            foreach (var category in categories)
            {
                if (BlackList.Contains(category.Id))
                {
                    continue;
                }
                var cats = new List <int>();
                cats.Add(category.Id);

                var categoryModel = new CategorySimpleModelCustom
                {
                    Id               = category.Id,
                    Name             = category.GetLocalized(x => x.Name),
                    SeName           = category.GetSeName(),
                    IncludeInTopMenu = category.IncludeInTopMenu,
                    ProductsCount    = _productService.GetNumberOfProductsInCategory(cats, _storeContext.CurrentStore.Id)
                };


                //number of products in each category
                if (_catalogSettings.ShowCategoryProductNumber)
                {
                    string cacheKey = string.Format(ModelCacheEventConsumer.CATEGORY_NUMBER_OF_PRODUCTS_MODEL_KEY,
                                                    string.Join(",", _workContext.CurrentCustomer.GetCustomerRoleIds()),
                                                    _storeContext.CurrentStore.Id,
                                                    category.Id);
                    categoryModel.NumberOfProducts = _cacheManager.Get(cacheKey, () =>
                    {
                        var categoryIds = new List <int>();
                        categoryIds.Add(category.Id);
                        //include subcategories
                        if (_catalogSettings.ShowCategoryProductNumberIncludingSubcategories)
                        {
                            categoryIds.AddRange(GetChildCategoryIds(category.Id));
                        }
                        return(_productService.GetNumberOfProductsInCategory(categoryIds, _storeContext.CurrentStore.Id));
                    });
                }

                if (loadSubCategories)
                {
                    var subCategories = PrepareCategorySimpleModels(category.Id, loadSubCategories, allCategories);
                    if (subCategories.Count > 0)
                    {
                        categoryModel.SubCategories.AddRange(subCategories);
                    }
                }
                if (categoryModel.ProductsCount > 0 || categoryModel.SubCategories.Count > 0)
                {
                    result.Add(categoryModel);
                }
            }

            return(result);
        }