How to add a new column in customer register?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
1 ano atrás
Thanks so much ILYAS PATEL, the new column is working in register page now. But looks like the value of groupowner I inputed during the customer registration cannot be saved to the database. Need to change something as well? :-) thanks again.
1 ano atrás
Your welcome 😊

If you want to store the groupowner you need to add this into the customer register POST METHOD

When you click on register add debug in post method so you will get idea and add the logic here just need to do this things before insert record
You need to assign model value into customer object
Customer.groupowner= model.groupowner
Rest insert code is already done
1 ano atrás
Regarding save the data to the database, what I have done are is:

Solution: Presentation/Nop.Web/Controllers/CustomerController.cs
#region Register
[HttpPost]
//GDPR
add the following code:
if (_customerSettings.groupownerEnabled)
                        await _genericAttributeService.SaveAttributeAsync(customer, NopCustomerDefaults.groupownerAttribute, model.groupowner);

rebuild/run, but the data has not been saved in the database, any idea?
1 ano atrás
It is also the issue of "if (_customerSettings.groupownerEnabled)",  I have fixed it via debugging, thanks so much ILYAS PATEL. Merry Christmas. :-)
1 ano atrás
That's great
Merry Christmas. :-)
1 ano atrás
Now I want to display the value of groupowner in the page of "My account - Customer info",  what I did are:
CustomerInfoModel.cs:
        [NopResourceDisplayName("GroupOwner")]
        public string groupowner { get; set; }
info.cshtml:
                    @if (Model.groupownerEnabled)
                    {
                        <div class="inputs">
                            <label asp-for="groupowner" asp-postfix=":"></label>
                            <input asp-for="groupowner" />
                            @if (Model.groupownerRequired)
                            {
                                <nop-required />
                            }
                            <span asp-validation-for="groupowner"></span>
                        </div>
                    }
CustomerValidator.cs
        RuleFor(x => x.groupowner).Length(0, 255);

Rebuild/Run, I only can see the page in "My account - Customer info", but no value/data in the column GroupOwner, I am sure it has data in the database. How to get the data out from the database? thanks.
1 ano atrás
Hi
you need to get the value from DB into the model using this method of factory
model = await _customerModelFactory.PrepareCustomerInfoModelAsync(model, await _workContext.GetCurrentCustomerAsync(), false);


please click here  it describe more detail where need to add the code

and one more thing, you need to verify that @if (Model.groupownerEnabled) this value must be true then only it can be displayed
1 ano atrás
Thanks for your quick reply. I debug the below from:
Solution: Presentation/Nop.Web/Controllers/CustomerController.cs

            var customer = await _workContext.GetCurrentCustomerAsync();
            if (!await _customerService.IsRegisteredAsync(customer))
                return Challenge();

            var model = new CustomerInfoModel();
            model = await _customerModelFactory.PrepareCustomerInfoModelAsync(model, customer, false);

After debugging, I can see:
customer:
    groupowner=12345, which is the data in the database
model:
    groupowner=null
    groupownerEnabled=true

Not sure why groupowner got to null after:
model = await _customerModelFactory.PrepareCustomerInfoModelAsync(model, customer, false);

I also checked CustomerInfoModel, the below have been put into the model.
Solution: Presentation/Nop.Web/Models/Customers/CustomerInfoModel.cs

[NopResourceDisplayName("GroupOwner")]
public string groupowner { get; set; }

anything I missed? thanks.
1 ano atrás
Hi
please verify this things

1. Add groupowner  assign in GET method in customer  PrepareCustomerInfoModelAsync
model.groupowner=customer.groupowner ( or table in which this field is stored ) 
model.groupownerEnabled=customer.groupownerEnabled


2. add in VIEW PAGE
 <div class="inputs">
                            <label asp-for="groupowner " asp-postfix=":"></label>
                            <input asp-for="groupowner " />
                          
                            <span asp-validation-for="groupowner "></span>
                        </div>

same as for another field

3. In POST METHOD you can assign like this
customer.groupowner=model.groupowner ( or table in which this field is stored ) 
customer.groupownerEnabled=model.groupownerEnabled


and add update query


Please verify this things you have added
1 ano atrás
Thanks for the quick update.

After setting the below in PrepareCustomerInfoModelAsync
Solution: Presentation/Nop.Web/Areas/Admin/Factoryies/CustomerModelFactory.cs
      customer.groupowner=model.groupowner
The data of groupowner can be displayed normally. However, when I changed the data to another value from page of "My account - Customer info", which cannot be saved successfully.

After debugging  the below code in CustomerController.cs,
Solution: Presentation/Nop.Web/Controllers/CustomerController.cs
model did not accept the new value, anything I missed here?


        public virtual async Task<IActionResult> Info()
        {
            var customer = await _workContext.GetCurrentCustomerAsync();
            if (!await _customerService.IsRegisteredAsync(customer))
                return Challenge();

            var model = new CustomerInfoModel();
//            model = await _customerModelFactory.PrepareCustomerInfoModelAsync(model, customer, false);
            model = await _customerModelFactory.PrepareCustomerInfoModelAsync(model, await _workContext.GetCurrentCustomerAsync(), false);
            return View(model);
        }

I also checked CustomerController.cs in admin side. I have put the below code in it, fyi.
Solution: Presentation/Nop.Web/Areas/Admin/Controllers/CustomerController.cs
public virtual async Task<IActionResult> Create(CustomerModel model, bool continueEditing, IFormCollection form)
......
                if (_customerSettings.groupownerEnabled)
                    await _genericAttributeService.SaveAttributeAsync(customer, NopCustomerDefaults.groupownerAttribute, model.groupowner);
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.