How to check customer role

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
3 years ago
Works in NC 4.2 as well
2 years ago
Hi,

I'm only finding this method in IWorkContext:

Task<Customer> GetCurrentCustomerAsync();

I'm also not finding the IsInCustomerRole method, I'm just finding the following method in CustomerService.cs class.

        public virtual async Task<bool> IsInCustomerRoleAsync(Customer customer,
            string customerRoleSystemName, bool onlyActiveCustomerRoles = true)

Can somebody help me? Thank you so much
2 years ago
In 4.40, it is in CustomerExtensions.cs file
here is the path :
Libraries\Nop.Core\Domain\Customers\CustomerExtensions.cs
Example of use:
customer.IsInCustomerRole("customer role system name", false)
2 years ago
Hi!

Thanks for your quick reply but in this file that you are telling me, I don't see that method.

Only the following methods appear:

bool IsSearchEngineAccount(this Customer customer)

bool IsBackgroundTaskAccount(this Customer customer)
2 years ago
IsInCustomerRoleAsync(...)
Is in nopCommerce_4.40.4_Source\Libraries\Nop.Services\Customers\CustomerService.cs

Searching the solution is useful for finding things :)
1 year ago
I'm using NC 4.40.

I need to add 5% to the displayed price if they are not logged in.  
I can do the add 5% part. But, in my _ProductBox.cshtml file, how can I tell the customer role or if they are logged in?
1 year ago
RE: "...add 5% to the displayed price"
Unless maybe if you are using "customer enters prices", just changing the "displayed price" in the .cshtml file is not going to affect how that price is recalculated when it gets added to the cart, etc.
1 year ago
True. But, if I can determine if they are a Guest I will increase the displayed price, hide the add to cart button and display a message that says they need to register/log in to see "their" price.
1 year ago
Hi you can check if the customer is GUEST User or not in the view page also if not doing more customize  then, you just need to use this service

 public virtual async Task<bool> IsGuestAsync(Customer customer, bool onlyActiveCustomerRoles = true)
        {
            return await IsInCustomerRoleAsync(customer, NopCustomerDefaults.GuestsRoleName, onlyActiveCustomerRoles);
        }


or you can call the same service in view page by passing work context

(await _customerService.IsGuestAsync(await workContext.GetCurrentCustomerAsync()))


using this you will get idea , that user is guest user or not and according to that you can add condition for the price
1 year ago
Thanks for your reply. But, I'm not sure how to use the code.  The below is the view page code. Where would I place the code you gave?
----------------------------------------------------------
@model ProductOverviewModel

@using Nop.Core
@using Nop.Core.Domain.Catalog
@using Nop.Core.Domain.Orders
@using Nop.Core.Domain.Tax


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


@inject CatalogSettings catalogSettings
@inject IWorkContext workContext
@inject IViewComponentSelector viewComponentSelector

@{

    //prepare "Add to cart" AJAX link
    var addtocartlink = "";
    var shoppingCartTypeId = (int)ShoppingCartType.ShoppingCart;
    var quantity = 1;

  


    if (Model.ProductPrice.ForceRedirectionAfterAddingToCart)
    {
        addtocartlink = Url.RouteUrl("AddProductToCart-Catalog", new { productId = Model.Id, shoppingCartTypeId = shoppingCartTypeId, quantity = quantity, forceredirection = Model.ProductPrice.ForceRedirectionAfterAddingToCart });
    }
    else
    {
        addtocartlink = Url.RouteUrl("AddProductToCart-Catalog", new { productId = Model.Id, shoppingCartTypeId = shoppingCartTypeId, quantity = quantity });
    }
}
<div class="product-item" data-productid="@Model.Id">
    
    <h2 class="product-title">
        <a href="@Url.RouteUrl("Product", new { SeName = Model.SeName })">@Model.Name</a>
    </h2>


    <div class="picture">
        <a href="@Url.RouteUrl("Product", new { SeName = Model.SeName })" title="@Model.DefaultPictureModel.Title">
            @if (viewComponentSelector.SelectComponent("LazyLoadImage") != null)
            {
                @await Component.InvokeAsync("LazyLoadImage", new (string Key, string Value)[]
{
                    ("src", Model.DefaultPictureModel.ImageUrl),
                    ("alt", Model.DefaultPictureModel.AlternateText),
                    ("title", Model.DefaultPictureModel.Title)
})
            }
            else
            {
                <img alt="@Model.DefaultPictureModel.AlternateText" src="@Model.DefaultPictureModel.ImageUrl" title="@Model.DefaultPictureModel.Title" />
            }
        </a>
    </div>




    <div class="details">
        @if (Model.ReviewOverviewModel.AllowCustomerReviews)
        {
            var ratingPercent = 0;
            if (Model.ReviewOverviewModel.TotalReviews != 0)
            {
                ratingPercent = ((Model.ReviewOverviewModel.RatingSum * 100) / Model.ReviewOverviewModel.TotalReviews) / 5;
            }
            <div class="product-rating-box" title="@string.Format(T("Reviews.TotalReviews").Text, Model.ReviewOverviewModel.TotalReviews)">
                <div class="rating">
                    <div style="width: @(ratingPercent)%">
                    </div>
                </div>
            </div>
        }
        <div class="description">
            @Html.Raw(Model.ShortDescription)
        </div>

        @if (catalogSettings.ShowSkuOnCatalogPages && !string.IsNullOrEmpty(Model.Sku))
        {
            <div class="sku">
                <span>PART #: @Model.Sku</span><br />

            </div>
        }
        <div class="add-info">

            @await Component.InvokeAsync("Widget", new { widgetZone = PublicWidgetZones.ProductBoxAddinfoBefore, additionalData = Model })
            <div class="prices">
                @if (!string.IsNullOrEmpty(Model.ProductPrice.OldPrice))
                {
                    <span class="price old-price">@Model.ProductPrice.OldPrice</span>
                }

                @if (!string.IsNullOrEmpty(Model.ProductPrice.Price))
                {
                    <span class="price actual-price">@Model.ProductPrice.Price</span>
                }
                else
                {


                    <span class="price actual-price">Register/Log In to see your price!</span>
                }





                @if (Model.ProductPrice.DisplayTaxShippingInfo)
                {
                    var inclTax = await workContext.GetTaxDisplayTypeAsync() == TaxDisplayType.IncludingTax;
                    //tax info is already included in the price (incl/excl tax). that's why we display only shipping info here
                    //of course, you can modify appropriate locales to include VAT info there
                    <span class="tax-shipping-info">
                        @T(inclTax ? "Products.Price.TaxShipping.InclTax" : "Products.Price.TaxShipping.ExclTax", Url.RouteUrl("Topic", new { SeName = await Html.GetTopicSeNameAsync("shippinginfo") }))
                    </span>
                }
                @if (!string.IsNullOrEmpty(Model.ProductPrice.BasePricePAngV))
                {
                    <div class="base-price-pangv">
                        @Model.ProductPrice.BasePricePAngV
                    </div>
                }
            </div>
            @await Component.InvokeAsync("Widget", new { widgetZone = PublicWidgetZones.ProductBoxAddinfoMiddle, additionalData = Model })
            <div class="buttons">
                @if (!Model.ProductPrice.DisableBuyButton)
                {
                    var addToCartText = T("ShoppingCart.AddToCart").Text;
                    if (Model.ProductPrice.IsRental)
                    {
                        addToCartText = T("ShoppingCart.Rent").Text;
                    }
                    if (Model.ProductPrice.AvailableForPreOrder)
                    {
                        addToCartText = T("ShoppingCart.PreOrder").Text;
                    }
                    <button type="button" class="button-2 product-box-add-to-cart-button" onclick="AjaxCart.addproducttocart_catalog('@addtocartlink');return false;">@(addToCartText)</button>
                }
            </div>
            @await Component.InvokeAsync("Widget", new { widgetZone = PublicWidgetZones.ProductBoxAddinfoAfter, additionalData = Model })
        </div>
    </div>
</div>
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.