This is the structure of my site Category1 --Product1 --Product2 Category2 --p1 --p2
I am trying to display the name of the category for each product in the Homepage Featured Product. 1.Is there any function to display the name based on productid? 2.I searched the forum and found how to get the category id based on product id. Is there any function to get the category name based on category id? @{ int currentProductId = Model.Id; var currentCategoryId = 0;
if (currentProductId != 0) { var categories = Nop.Core.Infrastructure.EngineContext.Current.Resolve<Nop.Services.Catalog.ICategoryService>().GetProductCategoriesByProductId(currentProductId); currentCategoryId = categories.Select(x => x.CategoryId).FirstOrDefault();
var name = Nop.Core.Infrastructure.EngineContext.Current.Resolve<Nop.Services.Catalog.ICategoryService>().GetCategoryById(currentCategoryId).Name;
The complete code
_ProductSmallBox.cshtml
@{ int currentProductId = Model.Id; var currentCategoryId = 0;
// Update current category id if (currentProductId != 0) { var categories = Nop.Core.Infrastructure.EngineContext.Current.Resolve<Nop.Services.Catalog.ICategoryService>().GetProductCategoriesByProductId(currentProductId);
currentCategoryId = categories.Select(x => x.CategoryId).FirstOrDefault(); var l = Nop.Core.Infrastructure.EngineContext.Current.Resolve<Nop.Services.Catalog.ICategoryService>().GetCategoryById(currentCategoryId).Name;