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:
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 ?
I need limit BlogPost.chtml to registered users. How can I do that?