How to display role based content in nopCommerce

There are many situations when store administrators would like to display different content on some of the web pages based on roles.  Let us assume: if a user is logged in with "role A", you might want that user to see only few options on the webpage. But if a user is logged in with "role B", on the same web page you might want that user to see all options (just like access control list). This is when this solution comes in handy as you can have one single webpage but different information on it based on the role.

For this example / demo, we will try to post a simple message in "My Account" page and display it based on the role.

1) Go to Nop.Web / Views / Customer / Info.cshtml

nopCommerce role based content

2) Open the "Info.cshtml" page and add this code at the top:

@using Nop.Core.Domain.Customers;
@using Nop.Core.Infrastructure;
@using Nop.Services.Customers;

Complete top section should look like this:

@model CustomerInfoModel
@using Nop.Web.Models.Customer;
@using Nop.Core;
@using Nop.Core.Domain.Customers;
@using Nop.Core.Infrastructure;
@using Nop.Services.Customers;

3) On the page, we will add the code that will display the content based on role like this:

@if (EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.IsInCustomerRole("RoleGoesHere")) { <p>content goes here</p> }

So, if we want a message for user with "Administrator" role, we will use this code:

@if (EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.IsInCustomerRole("Administrators")) { <p>The message is only for Admin role</p> }

4) In this case, we will use two messages: One for role "Registered" and the other for role "Administrator":

@if (EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.IsInCustomerRole("Administrators"))
    <p>The message is only for Admin role</p>
@if (EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.IsInCustomerRole("Registered"))
    <p>This is for "Registered" customer</p>

5) That's it - Save your page and go to the public store > login > my account

Currently I am logged in as admin with both roles assigned "Administrator" & "Registered":

nopCommerce role based content

This is what I see in "My Account" page:

nopCommerce role based content

Now, if I login as a customer with only "Registered" role, I see this:

nopCommerce role based content

About Author

Written By Lavish Kumar

Based out of New York, USA, Lavish Kumar is a full stack web developer by profession and founder of Striving Programmers, a trusted community for developers that offers a wealth of articles and forums to assist individuals with improving their software development skills.

Leave your comment
12/15/2015 5:24 AM
thank you very much Lavish Kumar. this article is great. may you help me the same technique for nopcommerce BlogPost.chtml ?
12/20/2015 12:58 AM
Thank you for your comments / feedback. What exactly are you trying to accomplish with this technique in nopCommerce blog section?
12/20/2015 4:49 AM
Thank you for response. I would like, some of my web site visitors (for example Customers role), can see blog contents based on some of blog tags and these contents, be hide for others.
12/20/2015 5:26 AM
This may require a lot of customization in the code. I would recommend looking into the ACL (access control list) and see how it allows users (based on different roles) to view / access different sections of the website. You can apply similar approach for this requirement.
2/23/2016 6:52 PM
Great blog thanks for sharing
2/23/2016 9:55 PM
@Darrell - Thank you for the comments / feedback :)
5/4/2016 7:55 PM

what is my requirements ?
I have a number of categories which needs to show by customer role, like x category to show all users but not trade users and y category to show only to trade users.

What i have tried ?
I added a customer role as 'Trade User' and set its system name as 'tradeuser'. Then i set x category ACL to Guest and Registered user and y category ACL to Registered user and Trade user. Then register a user and go to admin then set its role to Trade user. (I have to set its role to registered user also because it is not allowing to save for only Trade user role to a customer)

What problems are encountered ?
when i checked website (Front End) without login as a guest user, i found there is only x category are displaying which ACL selected as Guest and Registered user, which is correct. But when i login with a customer which role is Trade user, then i found there is both x and y category are displaying which is wrong and fulfill my requirement.

I thought, it will be solved by adding some additional conditions in code to displaying categories, but i think this will be not a good solution because i have to change complete nopcommerce functionality wherever needs.

Could you please suggest me for where i am doing wrong or is there any way in nopcommerce to set this requirement ?
5/9/2016 11:44 PM
@Hemant, what you are doing is correct as ACL is the best option that will help you in displaying categories based on the customer roles. The only problem I see in your case is that if you are allowing all categories for "Registered" role then all the register users will be able to view the categories.
8/9/2016 3:05 AM
Lavish Kumar
I need limit  BlogPost.chtml to registered users. How can I do that?