Product listed 2 or 3 times per Category in Admin

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 years ago
Hi,

I am using 3.5 and I find that when viewing a Category in admin and clicking on the Products tab.  Products are listed multiple times. It's the same Product with same ID.


Here is the ProductList Code from CategoryController in Admin


       [HttpPost]
        public ActionResult ProductList(DataSourceRequest command, int categoryId, int storeId)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageCategories))
                return AccessDeniedView();

            var productCategories = _categoryService.GetProductCategoriesByCategoryId_PerStoreId(categoryId, storeId,
                command.Page - 1, command.PageSize, true);
            var gridModel = new DataSourceResult
            {
                Data = productCategories.Select(x => new CategoryModel.CategoryProductModel
                {
                    Id = x.Id,
                    CategoryId = x.CategoryId,
                    ProductId = x.ProductId,
                    ProductName = _productService.GetProductById(x.ProductId).Name,
                    IsFeaturedProduct = x.IsFeaturedProduct,
                    DisplayOrder = x.DisplayOrder
                }),
                Total = productCategories.TotalCount
            };

            return Json(gridModel);
        }


And here is the GetProductCategoriesByCategoryId_PerStoreId from CategoryService


public virtual IPagedList<ProductCategory> GetProductCategoriesByCategoryId_PerStoreId(int categoryId, int storeId, int pageIndex, int pageSize, bool showHidden = false)
        {
            if (categoryId == 0)
                return new PagedList<ProductCategory>(new List<ProductCategory>(), pageIndex, pageSize);

            string key = string.Format(PRODUCTCATEGORIES_ALLBYCATEGORYID_KEY, showHidden, categoryId, pageIndex, pageSize, _workContext.CurrentCustomer.Id, _storeContext.CurrentStore.Id);
            return _cacheManager.Get(key, () =>
            {
                var query = from pc in _productCategoryRepository.Table
                            join p in _productRepository.Table on pc.ProductId equals p.Id
                            where pc.CategoryId == categoryId &&
                                  !p.Deleted &&
                                  (showHidden || p.Published)
                            orderby pc.DisplayOrder
                            select pc;

                if (!showHidden && (!_catalogSettings.IgnoreAcl || !_catalogSettings.IgnoreStoreLimitations))
                {
                    if (!_catalogSettings.IgnoreAcl)
                    {
                        //ACL (access control list)
                        var allowedCustomerRolesIds = _workContext.CurrentCustomer.CustomerRoles
                            .Where(cr => cr.Active).Select(cr => cr.Id).ToList();
                        query = from pc in query
                                join c in _categoryRepository.Table on pc.CategoryId equals c.Id
                                join acl in _aclRepository.Table
                                on new { c1 = c.Id, c2 = "Category" } equals new { c1 = acl.EntityId, c2 = acl.EntityName } into c_acl
                                from acl in c_acl.DefaultIfEmpty()
                                where !c.SubjectToAcl || allowedCustomerRolesIds.Contains(acl.CustomerRoleId)
                                select pc;
                    }
                    if (!_catalogSettings.IgnoreStoreLimitations)
                    {
                        //Store mapping
                        var currentStoreId = _storeContext.CurrentStore.Id;
                        query = from pc in query
                                join c in _categoryRepository.Table on pc.CategoryId equals c.Id
                                join sm in _storeMappingRepository.Table
                                on new { c1 = c.Id, c2 = "Category" } equals new { c1 = sm.EntityId, c2 = sm.EntityName } into c_sm
                                from sm in c_sm.DefaultIfEmpty()
                                where !c.LimitedToStores || currentStoreId == sm.StoreId
                                select pc;
                    }

                    //only distinct categories (group by ID)
                    query = from c in query
                            group c by c.Id
                                into cGroup
                                orderby cGroup.Key
                                select cGroup.FirstOrDefault();
                    query = query.OrderBy(pc => pc.DisplayOrder);
                }

                if (storeId > 0)
                {
                    //select products per store
                    query = from pc in query
                            join p in _productRepository.Table on pc.ProductId equals p.Id
                            join sm in _storeMappingRepository.Table
                            on new { c1 = pc.ProductId, c2 = "Product" } equals new { c1 = sm.EntityId, c2 = sm.EntityName } into c_sm
                            from sm in c_sm.DefaultIfEmpty()
                            where !p.LimitedToStores || sm.StoreId == storeId
                            orderby pc.DisplayOrder
                            select pc;

                    //query = from p in query
                    //        join sm in _storeMappingRepository.Table
                    //        on new { c1 = p.Id, c2 = "Product" } equals new { c1 = sm.EntityId, c2 = sm.EntityName } into p_sm
                    //        from sm in p_sm.DefaultIfEmpty()
                    //        where !p.Product.LimitedToStores || storeId == sm.StoreId
                    //        orderby p.DisplayOrder
                    //        select p;
                }

                var productCategories = new PagedList<ProductCategory>(query, pageIndex, pageSize);
                return productCategories;
            });
        }
7 years ago
I don't think it's the bug (moved to "development" forums).

By default we use "GetProductCategoriesByCategoryId" one. There's no such method - "GetProductCategoriesByCategoryId_PerStoreId". And your custom one is implemented wrong one (grouping is missing). That's why you have duplicates
7 years ago
Hi Andrei,

Thanks for pointing that out regarding the custom code. I've now implemented the grouping after the customized section and it's working 100%.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.