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
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”:
This is what I see in “My Account” page:
Now, if I login as a customer with only “Registered” role, I see this:
Looking for quality nopCommerce Web Hosting? Look no further than Arvixe Web Hosting!