Custom Registration based on customer role

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 years ago
Hi, I'm a new .Net developer trying to customize nopcommerce for clients. I'm really enjoying learning the ins and outs of nopcommerce so far.

Frequently I've been able to overcome issues by searching these forums, but no such luck so far on this.
I want to create a specialized registration page that works almost identical to Register.aspx (using the CustomerRegister.ascx user control).

Once a user registers on this custom page, I want them to be automatically added to a customer role once they are created as a new customer (the customer role is Wholesalers and I've already created it in the admin backend).

I know that this information is stored in the CustomerRole_Mapping table in the database, but I'm at a loss as to how to update/add to this table in the code behind.

At first I tried:

        string customerUserName = customer.Username;
        Roles.AddUserToRole(customerUserName, "Wholesalers");

but the AddUserToRole method isn't implemented :(
I'm using version 1.90 by the way.

Any ideas?
13 years ago
awpg wrote:
Hi, I'm a new .Net developer trying to customize nopcommerce for clients. I'm really enjoying learning the ins and outs of nopcommerce so far.

Frequently I've been able to overcome issues by searching these forums, but no such luck so far on this.
I want to create a specialized registration page that works almost identical to Register.aspx (using the CustomerRegister.ascx user control).

Once a user registers on this custom page, I want them to be automatically added to a customer role once they are created as a new customer (the customer role is Wholesalers and I've already created it in the admin backend).

I know that this information is stored in the CustomerRole_Mapping table in the database, but I'm at a loss as to how to update/add to this table in the code behind.

At first I tried:

        string customerUserName = customer.Username;
        Roles.AddUserToRole(customerUserName, "Wholesalers");

but the AddUserToRole method isn't implemented :(
I'm using version 1.90 by the way.

Any ideas?


Check: https://www.nopcommerce.com/boards/t/7837/automatic-customer-role-setup.aspx
There I PMed amplify and he replied the following:
amplify  
To:  eadameg  
Subject:  Re: Can you share the code for Automatic Customer Role Set Up  
Message:  I can but it's nothing fancy. Here is what I do with this:

1. Configure customer roles.
2. Added a textbox on the registration page that is manditory for the customer to fill out during registration.
3. Upon registration submission, SQL statements are executed that write the customer role ID /customer ID to the appropriate table and I am done.

Do you still want it?
13 years ago
You can use,

CustomerService.AddCustomerToRole(customer.CustomerId, 1);

it uses the id of the role, in this example "Global administrators = 1"

Regards,
Don
13 years ago
Don's code worked like a charm, thank you!
12 years ago
Thanks for your help Don.  I'm currently working in 2.1 and need the same exact solution.  It doesn't appear that the same control exists in the new version.
Do you know how to do the same thing now?
11 years ago
eric.shafer wrote:
Thanks for your help Don.  I'm currently working in 2.1 and need the same exact solution.  It doesn't appear that the same control exists in the new version.
Do you know how to do the same thing now?


eric.shafer,

I'm unsure if you still need this functionality but found where to do it and thought I'd post it in case others need the information. this is based off of nopCommerce 2.50.

Inside of VS navigate to: Libraries >> Nop.Services >> Customers >> CustomerRegistrationService.cs

Original code looks as so: (at line 201)
//add to 'Registered' role
            var registeredRole = _customerService.GetCustomerRoleBySystemName(SystemCustomerRoleNames.Registered);
            if (registeredRole == null)
                throw new NopException("'Registered' role could not be loaded");
            request.Customer.CustomerRoles.Add(registeredRole);
            //remove from 'Guests' role
            var guestRole = request.Customer.CustomerRoles.FirstOrDefault(cr => cr.SystemName == SystemCustomerRoleNames.Guests);
            if (guestRole != null)
                request.Customer.CustomerRoles.Remove(guestRole);


Modified code:
//add to 'Registered' role
            var registeredRole = _customerService.GetCustomerRoleBySystemName(SystemCustomerRoleNames.Registered);
            if (registeredRole == null)
                throw new NopException("'Registered' role could not be loaded");
            request.Customer.CustomerRoles.Add(registeredRole);
           var customRole = _customerService.GetCustomerRoleBySystemName("string");
            request.Customer.CustomerRoles.Add(customRole);

            //remove from 'Guests' role
            var guestRole = request.Customer.CustomerRoles.FirstOrDefault(cr => cr.SystemName == SystemCustomerRoleNames.Guests);
            if (guestRole != null)
                request.Customer.CustomerRoles.Remove(guestRole);


Please note the the bold and underlined area "string". Just place the name of the role you want. if you created the customer role in the admin and named it, Reseller than you would put, var customRole = _customerService.GetCustomerRoleBySystemName("Reseller");

Of course you can rename var customRole to something more meaningful like var resellerRole = ...

Hope that helps someone.
11 years ago
Hi,

I have added different customer roles to the Registration page. I want customer to be mapped to role which he has chosen, but right now  he is getting mapped to all the roles. I know it is happening because of the following code :

//add to 'Registered' role
            var registeredRole = _customerService.GetCustomerRoleBySystemName(SystemCustomerRoleNames.Registered);
            /*if (registeredRole == null)
                throw new NopException("'Registered' role could not be loaded");*/
            request.Customer.CustomerRoles.Add(registeredRole);

            //Added Code

            var customRole = _customerService.GetCustomerRoleBySystemName(SystemCustomerRoleNames.StoreManager);
            request.Customer.CustomerRoles.Add(customRole);

            var customRole1 = _customerService.GetCustomerRoleBySystemName(SystemCustomerRoleNames.Wholesalers);
            request.Customer.CustomerRoles.Add(customRole1);

            //end

But i don't know how to get solve this? How do I map customer to the role which he has chosen at the time of registration. Need your help on this.

Please Reply

Thanks,
Ricky
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.