Get Categories on ProductTemplate.Simple.cshtml

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 years ago
I've created a category for Free Shipping.  Now I want to display the standard NOP Free Shipping div on the Product Detail page whenever the product is in the category Free Shipping.  What is the simplest to look up whether the product is a member of the Free Shipping category?

Thank you in advance for your advice and assistance.

JohnJ
7 years ago
The easiest way is to do something like:

@{
    var productCategories = EngineContext.Current.Resolve<ICategoryService>().GetProductCategoriesByProductId(Model.Id);
    var isInFreeShipping = productCategories.Any(c => c.Category.Name.Equals("Free Shipping"));
}
@if ((Model.FreeShippingNotificationEnabled && (Model.IsFreeShipping || isInFreeShipping)) || !String.IsNullOrWhiteSpace(Model.DeliveryDate))
{
    <div class="delivery">
        @if (Model.FreeShippingNotificationEnabled && (Model.IsFreeShipping || isInFreeShipping))
        {
            <div class="free-shipping">@T("Products.FreeShipping")</div>
...


(_DeliveryInfo view)
7 years ago
Mariann,

As always your replies are spot on.  Thank you for the code.  I inserted it into the ProductTemplate.Simple.cshtml view, where it works great.

One last question.  I really wanted to have the Free Shipping category non-published.  GetProductCategoriesByProductId only returns published categories (by good design).  

Is there a way to query against a non-published category, even if it means querying directly against the categoryId of the Free Shipping category?

If not, I'll live with having the category published.

Thanks again for the quick resolution.

JohnJ
7 years ago
cnsJohn wrote:
One last question.  I really wanted to have the Free Shipping category non-published.  GetProductCategoriesByProductId only returns published categories (by good design).  

Is there a way to query against a non-published category, even if it means querying directly against the categoryId of the Free Shipping category?

The GetProductCategoriesByProductId method takes a second optional parameter called showHidden that also returns unpublished categories. So just pass a second parameter with a value of true:

GetProductCategoriesByProductId(Model.Id, true)
7 years ago
THANKS!!

I'll give it a try.

JohnJ
7 years ago
pete,

The additional parameter worked perfectly.  

Thank you.

JohnJ
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.