NopCommerce : Display best seller products on category home page
-
21-12-2019 - |
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?
Solution
- You should create a new action method for bestsellers, in which you will pass category id.
In this method you should use the BestSellersReport method like here:
_orderReportService.BestSellersReport(storeId: _storeContext.CurrentStore.Id, categoryId: categoryId)
This new method is very similar to HomepageBestSellers in the CatalogController. See how it's done.
- 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