How to add custom customer attributes / fields in nopCommerce

There are times when some nopCommerce store admins finds themselves in a situation when the default registration form fields are not enough for their needs (if they would like to manage more information of the customers). In reality, it can be a very difficult task for a non-technical store admin to dive into the code and make the required modifications (in code and the database) for adding custom fields. A lot of users were facing this issue in the previous/old versions of nopCommerce as there was no way to accomplish  this unless the user makes the modifications in the code / database.

In order to overcome this issue, a feature (option) was added to the nopCommerce project in the administration section that allows store admins to add custom fields in a user-friendly way.

Today we will go over this long awaited feature that was added to nopCommerce !

In order to add custom fields (which can be added easily in the "Register" page", go to:
Administration > Configuration > Settings > Customer Settings





Go to "Customer form fields" tab and at the bottom you will see the option for adding "Custom customer attributes" - Click on "Add"





For this example, I am going to add a field in  the register page as "Select a security question" (in a form of drop down menu)




Hit "save and continue" and then click on "Attribute values" tab for adding the dropdown values.

Click on "Add a new customer value" button to fill in the values.



Make sure you "SAVE" all  the values.

Now when you logout and go to the public store > Register page

You should see your custom field like this:



In the case of this example - You can simply add a text box for entering the values for the security question (in same pattern as discussed above).

Hence, you can add custom fields on your "Register" page according to your requirements / needs.

Hope it helps :)


Note: I have used nopCommerce 3.4 for this example


UPDATE: There is an issue (bug) in nopCommerce 3.40 that limits the values to be saved in "custom textbox" field (lengthwise) and user can only save upto certain number of characters (I think 4-5 characters only).

FIX / SOLUTION:
Go to: Nop.Web/Views/Customer/_CustomerAttributes.cshtml

Open the file "_CustomerAttributes.cshtml" and find the following code:

<input name="@(controlId)" type="text" class="textbox" id="@(controlId)" @(!String.IsNullOrWhiteSpace(attribute.DefaultValue) ? " value=" + attribute.DefaultValue : null) />


Change it to this:

<input name="@(controlId)" type="text" class="textbox" id="@(controlId)" value="@attribute.DefaultValue" />

Save changes and try saving a longer value in your custom textbox - Now, you should be able to save any value (of any length).

Hope it helps!

About Author

Written By Lavish Kumar

Based out of New York, USA, Lavish Kumar is a full stack web developer by profession and founder of Striving Programmers, a trusted community for developers that offers a wealth of articles and forums to assist individuals with improving their software development skills.

Leave your comment
Comments
11/11/2014 2:50 PM
sir ,is  this field add in database?? if yes then in which table???
11/12/2014 3:12 AM
Yes, all the custom fields are added in the database.

1) The custom fields are added to "CustomerAttribute" table
2) The value for these custom fields are added to "CustomerAttributeValue" table
10/20/2015 6:39 AM
Can you add custom fields but not display them, I want to add a field but only have it be edited in the admin, it should not display to the user.
2/15/2016 6:26 PM
Custom fields are in the XML
2/15/2016 6:28 PM
or NopCommerce saves in database in XML format?
Exuse me )
2/16/2016 9:33 PM
@Randell, you will have to make some custom modifications in order to accomplish that.
2/16/2016 9:34 PM
@Serguei, nopCommerce use MS SQL Server Database...
2/27/2016 1:08 AM
1.How to add validation message for custom field in registration.
2. Change id and name of custom attribute.

3/14/2016 4:02 AM
I tried this using a text box, and I entered "This is a test" into the text box field. Yet when it saved, it only saved the word "This". Why is this? Is there a definition for the length of the textbox value?
3/14/2016 6:35 PM
@Jon - What version of nopCommerce are you using?
3/14/2016 11:07 PM
V 3.40
3/14/2016 11:40 PM
Thanks for the information Jon - I will test this on my side and get back to you...
3/14/2016 11:47 PM
Thanks very much, I look forward to your findings!
3/15/2016 1:38 PM
@ahmed - Here are the answers to your questions:

1.How to add validation message for custom field in registration.

ANSWER: By default, you can only add "required" validation to your custom field. If you are looking to add different type of validation then the best way to do that is via Javascript on the client side. Simply inspect the custom field on your browser, get the field information and add the validation in your view.


2. Change id and name of custom attribute.

ANSWER: You cannot change the ID or name of custom attribute because if it dynamic and making changes to the default code (view) might break the functionality. If your requirement is to add a custom field with a specific ID or name attribute then you will have to make customization in the code and not use this dynamic feature of adding custom field. You can customize the code and add the field directly to the customer database table and map everything accordingly.

Hope it helps!
3/15/2016 1:56 PM
@Jon - I did look into the code and tested the custom textbox field for you. You are absolutely correct - The textbox limits the length and it is a bug.

I have updated this article and posted the "fix". Please apply it to your solution and it should resolve your problem.

Thanks for reporting the bug!
3/15/2016 11:40 PM
Lavish, thank you very much, that takes care of the issue!