Close

NopInstantSearch limited to customer roles not working

Monitor36400
6 years ago
#14158 Quote
Avatar
  • 2
We are using the Element Theme at v3.90.
We want to only show the 'Search' button in the header after login. From Configuration - Plugins - Local Plugins - Nop Instant Search - Edit we have set 'Limited to customer roles' to only include 'Administrators' and 'Registered'. But the Search button in the header still appears when no user is logged on. How can we condition the display of the 'Search' button?
anton_ivanov
6 years ago
#14159 Quote
Avatar
  • Moderator
  • 277
Hello,

Limiting the Instant Search plugin to only certain roles works. The Instant Seach is not displayed on your store if the customer role is not right but then the nopCommerce original search is displayed.

If you want your search button to not be shown in your public store for some customer roles you will need to do some code modifications. You will need to edit the ~/Views/Catalog/SearchBox.cshtml and get the current customer roles like this:

var customerRoles = EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.CustomerRoles;

(you will need @using Nop.Core; @using Nop.Core.Infrastructure; also)

After that, you will need to check if the user has the correct customer role and show/hide the search depending on that.

Hope that helps!

Regards,
Anton Ivanov
Nop-Templates.com
Monitor36400
6 years ago
#14163 Quote
Avatar
  • 2
Thank you for the information.
This did not exactly answer what we wanted but gave us enough details to be able to achieve what we wanted. The ~/Views/Catalog/SearchBox.cshtml view controls what happens when the Search box is visible. What we wanted was to actually hide the search button in the header so a user could not even get to the Search box.
Here is what we did in case it is of use to anyone else. We edited ~/Themes/Element/Views/Shared/_AirVariant1.cshtml.
Added
@using Nop.Core;
@using Nop.Core.Domain.Customers;
@using Nop.Core.Infrastructure;
@using Nop.Services.Customers;
  bool customerHasRoleAdministrator = EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.IsInCustomerRole("Administrators");
  bool customerHasRoleRegistered = EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.IsInCustomerRole("Registered");


Then placed this condition:
  
@if (customerHasRoleAdministrator||customerHasRoleRegistered)
  {}


around            
<div class="search-wrap">
                <span>@T("Search")</span>
            </div>


This gave us the desired effect.