Is there a way to get the topic from the Product or how would I do this coding it?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 years ago
Is there a way to get and print out the category of the product?  I just want to see if I can print out the product on the item on the category page in the actual product item box on the category page.  Any pointers on how to do this?
I am comfortable using C#/Razor etc. I just need to know the parts involved and any sample code if possible.
7 years ago
I don't quite understand. Are you saying that you want to be able to print the whole category page, including all products on the page, or are you saying you want to just print one of the product boxes on the category page, individually?
7 years ago
Sorry, no, I just want to print the Category name not the Category page itself.  
The I have plans on using the name for some UI stuff, but I first just need to print the category name out on the actual product box itself. See the text in red.

See picture, See the text in red.

https://lh6.googleusercontent.com/7iWqP-4BhJBanmzAxZrxZou-Z2I5Zc8vOFyIyFqamSPqt0H6g__IcBU01ML8hsKxiUHix9BdV0p3FlU=w1819-h777-rw
7 years ago
Hello,

In order to get the Category Name and use it in the product box you will need to do the following things:

Firstly, open the ~/Views/Catalog/CategoryTemplate.ProductsInGridOrLines.cshtml and add the following line of code:

    ViewBag.ExtraField = Model.Name;

After that, you will need to open the ~/Views/Shared/_ProductBox.cshtml and add the following lines:

    var categoryName = "";
    if (ViewBag.ExtraField != null)
    {
        categoryName = ViewBag.ExtraField;
    }


Now you assign the value from the extraField to the categoryName variable only if the extraField is not null. The value of the extraField is not null only if you are on the category details page.
Then you should use the categoryName variable and display the name of the category wherever you like.

Hope that helps!

Regards,
Anton
7 years ago
@Nop-Templates.com

That works great when products are in the category! BUT, what about if I allow subcategory products to be seen on the top level category page?

Can I get the subcategory product's categories outputted to?

Let's say top Category is Electronics, but the Sub Categories are Phones, Stereos, and Computers

Electronics
- SubCat: Phones
- SubCat: Stereos
- SubCat: Computers

If I allow the setting for Categories to show Sub Categories, how would I get the subcategory Name onto the products showing on the subcategory page.


https://lh4.googleusercontent.com/hEYL_wfvpdQTm_a_MjaRZcZweAf5drCPGtKp8Ld7SXgLp9UVf9Son4maK1ytQLJ4fXff5Xp3ZUXwEGo=w1819-h777-rw
7 years ago
Sorry, image link was not shared properly, here is the desired effect:

https://lh6.googleusercontent.com/ihO-0w30IrPUx2v66D86HKx37UHgO5utx5M0bl-rsRRWhK6jLQqlOTaNmzJU015P1SscQr5KldKn-dM=w1819-h777-rw
7 years ago
Hello,

Unfortunately, you cannot display the products subcategory if you are using the code I gave you. In fact, you should modify the nopCommerce source code, if you want to show the subcategory in the situation you are in.

Currently, there is no way to know from what category the product is in the _ProductBox.cshtml, you can only know what category called it, if that makes sense.

Maybe modifying the ProductOverviewModel, so that it has a property for the categories it's mapped to or using a custom model created by you would do the trick.

Regards,
Anton
7 years ago
Hey Anton,

We actually figured out a way to do it in the CategoryTemplate.ProductsInGridOrLines.cshtml view.

We go against the Category service directly and attach category to our Item box since each ProductBox partial is wrapped in the "@foreach (var product in Model.Products)".

Just in case you or anyone else is curious, this is how we did it anyways.  Should probably be done in the controller, but that seemed like a lot and was a safe bet to just to it this way in the view.



<div class="@(Model.PagingFilteringContext.ViewMode == "list" ? "product-list" : "product-grid")">
                <div class="item-grid">
                    @foreach (var product in Model.Products)
                    {
//BEGIN GET CATEGORY FOR PRODUCT
                    var categoryName = "";
                    var categoryService = EngineContext.Current.Resolve<Nop.Services.Catalog.ICategoryService>();
                    var categoryList = categoryService.GetProductCategoriesByProductId(product.Id);
                    foreach (var cat in categoryList) {
                            categoryName = @cat.Category.Name;
                        }

//END GET CATEGORY FOR PRODUCT
@*THEN SET THE CATEGORY ON THE PRODUCT BOX PARTIAL WRAPPER*@
                        <div class="item-box" data-categoryname="@categoryName.Replace(" ", "")">
                            @Html.Partial("_ProductBox", product)
                        </div>
                    }
                </div>
            </div>
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.