Question

I am using NopCommerce. I want to display top 3 bestseller products on category home page.

I got an idea that on view side CategoryTemplate.ProductsInGridOrLines.cshtml and controller side CatalogController.cs > HomepageBestSellers method are used.

I have passed category id as a parameter to HomepageBestSellers method. This category id is passed to BestSellersReport method as parameter.

My question is how can I use category id to display best seller products on category home page?

Was it helpful?

Solution

  1. You should create a new action method for bestsellers, in which you will pass category id.
  2. In this method you should use the BestSellersReport method like here:

    _orderReportService.BestSellersReport(storeId: _storeContext.CurrentStore.Id, categoryId: categoryId)

  3. This new method is very similar to HomepageBestSellers in the CatalogController. See how it's done.

  4. And you should create a view, like Views\Catalog\HomepageBestSellers.cshtml and show it where you need.

OTHER TIPS

Best Seller Product Code In NopeCommerce

#region bestsellers and products

        [ChildActionOnly]
        public ActionResult BestSellProduct(int categoryId)
        {

            //load and cache report
            var report = _orderReportService.BestSellersReport(storeId: _storeContext.CurrentStore.Id, categoryId: categoryId);


            //load products
            var products = _productService.GetProductsByIds(report.Select(x => x.ProductId).ToArray());
            //ACL and store mapping
            products = products.Where(p => _aclService.Authorize(p) && _storeMappingService.Authorize(p)).ToList();
            //availability dates
            products = products.Where(p => p.IsAvailable()).ToList();

            if (!products.Any())
                return Content("");

            //prepare model
            var model = PrepareProductOverviewModels(products, true, true, categoryId).ToList();
            return PartialView(model);
        }
        #endregion
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top