Display Category name in Homepage Featured Product

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 anni tempo fa
I am using Nopcommerce 2.65.

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();
        
    }
    }
11 anni tempo fa
ooops
found out by myself :)

Add this line to the code

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;
          
    }
    }
11 anni tempo fa
this isn't a feature/option in the 2.65? i SWEAR i had my categories displayed on my site, without code, but i'm running, 2.2
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.