Show the number of distinct products besides each category

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
1 year ago
Hello,
I want to Show the number of distinct products besides each category BUT not the Parent Category.

The count on the home page shows Bed (0) (Where there is no product in any of the sub categories of Beds)

Dining (10)  (Where different sub categories have different counts) and so on.
I have enabled the option to include the total in the main category. If this is disabled then the count shows (0) obviously because there is no product in the parent category, they are all in the sub categories.

I want only the Sub Categories to show the count and not the Root/Main Categories. So the home page should show Beds   Dining  Rugs Chairs etc. without (0) or any other count.

Please help
1 year ago
what is your nopCommerce version? I haved checked nop v4.40 & nop v4.50, it's already been calculated distinct products
public virtual async Task<int> GetNumberOfProductsInCategoryAsync(IList<int> categoryIds = null, int storeId = 0)
        {
            //validate "categoryIds" parameter
            if (categoryIds != null && categoryIds.Contains(0))
                categoryIds.Remove(0);

            var query = _productRepository.Table.Where(p => p.Published && !p.Deleted && p.VisibleIndividually);

            //apply store mapping constraints
            query = await _storeMappingService.ApplyStoreMapping(query, storeId);

            //apply ACL constraints
            var customer = await _workContext.GetCurrentCustomerAsync();
            var customerRoleIds = await _customerService.GetCustomerRoleIdsAsync(customer);
            query = await _aclService.ApplyAcl(query, customerRoleIds);

            //category filtering
            if (categoryIds != null && categoryIds.Any())
            {
                query = from p in query
                        join pc in _productCategoryRepository.Table on p.Id equals pc.ProductId
                        where categoryIds.Contains(pc.CategoryId)
                        select p;
            }

            var cacheKey = _staticCacheManager
                .PrepareKeyForDefaultCache(NopCatalogDefaults.CategoryProductsNumberCacheKey, customerRoleIds, storeId, categoryIds);

            //only distinct products
            return await _staticCacheManager.GetAsync(cacheKey, () => query.Select(p => p.Id).Count());
        }
1 year ago
Hello Rashed,

It does show the number of products BUT I do not want this on the Main/Root Category.

It either shows the total of all products in the Category and Sub Category i.e. Beds (50)
OR it shows Beds (0) ------> This is when I exclude the sub category Total.

I want it to display Just Beds without (0)

Please see my website https://www.nestwow.com/

The version of nop commerce is 4.40.4
1 year ago
* change this setting catalogsettings.showcategoryproductnumberincludingsubcategories to true to include sub-category product count at the main category.
from Admin -> Configurations -> Settings -> All settings (advanced)

* or add those condition here src\Presentation\Nop.Web\Views\Shared\Components\CategoryNavigation\Default.cshtml and here src\Presentation\Nop.Web\Views\Shared\Components\TopMenu\Default.cshtml to remove (0) beside category
   @if (lineModel.Category.NumberOfProducts.HasValue && lineModel.Category.NumberOfProducts.Value > 0)
                {
                    <text> </text>@T("Categories.TotalProducts", lineModel.Category.NumberOfProducts.Value)
                }
1 year ago
Thanks Rashed,

That has helped, I changed the coding in the database. This has taken away the (0). It has also taken away the (0) from the sub categories as well.

BUT I want the Count in the Sub Categories even if it is (0)

Thanks
I changed the other settings to take away the count from top menu
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.