Reputation: 900
I'd like to be able to show/hide certain payment methods based on user roles.
In Hotcakes under the "_DisplayPaymentMethods" view set I see a switch statement with "payMethod.MethodId" as the expression. Inside each case I'd like to set an if statement that checks if the current user has "x" role.
How can I access the user roles from this view set?
Upvotes: 1
Views: 95
Reputation: 1680
You'll find the answer in the Hotcakes Commerce documentation area. I've included it here for posterity on SO as well.
Security roles are used for a large number of purposes in both the e-commerce and CMS parts of your website. On occasion, you might want to re-purpose these roles to do something dynamic with your views. One example might be to only show the Add to Cart button to a specific role. We'll use that use case for this example.
First, you'll need a plan. In this plan, we're going to make the Add to Cart button available to all people that are logged in and part of the "VIP-Customer" security role. (This isn't a built-in role. It's made-up for this code sample. You can create and use whatever roles that you want.)
Add the code below to the header area of the view that you want to edit, such as the _ProductDetails.cshtml view.
@functions
{
private bool IsVipCustomer()
{
var customer = DotNetNuke.Entities.Users.UserController.Instance.GetCurrentUserInfo();
if (customer != null && customer.UserID > 0)
{
return customer.IsInRole("VIP-Customer") || customer.IsInRole("Administrators");
}
return false;
}
}
In this code sample below, we're checking to see if the role matches what we expect. If it does, we show the Add to Cart button. You can add and use this code anywhere you want, as long as it has the function we created above in the same view file.
@if (IsVipCustomer())
{
<input type="submit" id="addtocartbutton" value="@Localization.GetString("AddToCart")" class="dnnPrimaryAction largeButton fullCartButton" />
}
We hope this helps you as a baseline example for doing anything dynamic with views that involves the security roles in the CMS.
Upvotes: 1